フィルターのクリア

how to add legend values automatically ?

38 ビュー (過去 30 日間)
tomer polsky
tomer polsky 2019 年 4 月 5 日
コメント済み: Star Strider 2019 年 4 月 7 日
how to add legend automatically : for exmaple this is my code . the question is how to write the legend without writting every time it manually ?
clc;clear all;close all
t=linspace(0,7*pi,300);
for i= [ 2 3 4 5 ]
plot(t,cos(t+0.1*pi*i));
xticks(0:pi:7*pi)
xticklabels((""+(0:7))+"\pi")
hold on;
legend ('0.2*pi','0.3*pi','0.4*pi','0.5*pi')
end

採用された回答

Star Strider
Star Strider 2019 年 4 月 5 日
Try this (considerably simplified version of your code):
t=linspace(0,7*pi,300);
i= [ 2 3 4 5 ];
plot(t,cos(pi*0.1*i(:)*t));
xticks(0:pi:7*pi)
xticklabels((""+(0:7))+"\pi")
legend (sprintfc('%.1f\\pi',i*0.1))
The sprintfc function is undocumented although quite useful. You can also use the compose function to do the same thing here.
  2 件のコメント
tomer polsky
tomer polsky 2019 年 4 月 7 日
thank you for your help !!!
Star Strider
Star Strider 2019 年 4 月 7 日
As always, my pleasure!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLegend についてさらに検索

タグ

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by