Add legend to for loop

12 ビュー (過去 30 日間)
AKSHAY DESHMUKH
AKSHAY DESHMUKH 2021 年 4 月 22 日
コメント済み: AKSHAY DESHMUKH 2021 年 4 月 22 日
I am trying to add legend to for loop and I need my legend to have variables used to plot graphs.
fig1 = figure()
ax1 =axes('Parent',fig1)
for C = 1:36
for I = [1:5]
%Cl Vs Alpha
index = 1;
XVAR = 'Alpha';
YVAR='CL';
MACH = 0.27;
ALTITUDE = 0;
legend(sprintf('TEST_AERO%d_%d',I , C))
hold on
TEST_AERO_FINAL.(sprintf('TEST_AERO%d_%d',I , C)).DES_SPACE{index}.plot(XVAR,YVAR,MACH,ALTITUDE,ax1)
end
end

採用された回答

DGM
DGM 2021 年 4 月 22 日
編集済み: DGM 2021 年 4 月 22 日
Generally, it's best if you specify the plot object handle when you call legend(). Since all of your plotting code is missing, consider the simplified example:
numberofplots=3;
h=zeros(numberofplots,1); % preallocate handles vector
legendstrings=cell(numberofplots,1); % preallocate legend strings
for n=1:numberofplots
x=rand(10,1); % garbage example data
y=rand(10,1);
h(n)=plot(x,y); hold on; % store this plot handle
legendstrings{n}=sprintf('X1 = %f',x(1)); % store this legend string
end
% the call to legend() doesn't need to be in the loop
legend(h,legendstrings,'location','northwest');
  1 件のコメント
AKSHAY DESHMUKH
AKSHAY DESHMUKH 2021 年 4 月 22 日
Thank you!

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

その他の回答 (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