How can i add legend in a for loop after each plot ?
17 ビュー (過去 30 日間)
古いコメントを表示
In below code legend got updated each times thereby leaving me with only the last legend but i want is like 1st iteration plot and its legend and next plot and its legends ;thereby all legends should be there.
for i=2:2:20
y=(P(:,1));
plot((P(:,i+1)),y,'color',rand(1,3),'marker','s')
h{i}=char(headings(i));
legend(h(i))
end
0 件のコメント
採用された回答
Walter Roberson
2017 年 6 月 11 日
If you are using a fairly new version of MATLAB then see https://www.mathworks.com/help/matlab/ref/legend.html#bvk3tnk-1
for i=2:2:20
plot( P(:,i+1), P(:,1), 'color', rand(1,3), 'marker', 's', 'DisplayName', headings{i});
end
legend('show')
For older versions of MATLAB,
for i=2:2:20
plot( P(:,i+1), P(:,1), 'color', rand(1,3), 'marker', 's', );
end
legend( headings(2:2:20) );
or
for i=2:2:20
plot( P(:,i+1), P(:,1), 'color', rand(1,3), 'marker', 's', );
h{i} = headings{i};
end
legend( h(2:2:20) );
9 件のコメント
Walter Roberson
2020 年 7 月 2 日
load (txt{i})
Don't Do That!
If you load() a text file with no output, it will create a variable whose name is the same as the basic file name, and then you would have to use dynamic programming methods to get at the variable.
If you load() a .mat with no output, it will create one variable for each variable in the file, and then you either have to use dynmaic programming methods to get at the variables or else "just know" what the names were.
You should assign the result of load() to a variable. If it was a text file, then the result will just be a numeric array. If it was a mat file then the result will be a struct with one field for each variable.
filename = 'TR_LIST.xlsx';
[num,txt,raw] = xlsread(filename);
numtxt = numel(txt);
DATA1 = zeros(1, numtxt);
DATA2 = zeros(1, numtxt);
for i = 1 : numtxt
datafile = load(txt{i});
DATA1(i) = fix(datafile.ALT); %Save the Altitudes values
DATA2(i) = datafile.MACH; %Saves the Mach numbers
IND = datafile.IND;
legent = sprintf('ALT %d m MACH %f', DATA1(i), DATA2(i));
plot(datafile.THRN(IND), datafile.NGN(IND), 'DisplayName', legent);
hold all
end
hold off
xlabel('Corrected Gas Generator Speed')
ylabel('Corrected Thrust')
legend show
その他の回答 (1 件)
Fabio Abbà
2023 年 5 月 4 日
I just googled this question and found this topic... It has beena problem for me for a long time and now i founded a solution...
...reading the help of "legend" and assigning the output of the function to a variable you can be able to modify "live" the legend created in automatic, as far as you first plot and the modify it.
this is because once you enable the legend() on a plot, with the AutoUpdate option turned on, Matlab will automatically create a new legend once you plot a new line.
See the following example:
x = linspace(0,pi);
figure
my_legend = legend();
hold on, grid on
for i=1:5
plot(x,cos(i*x))
my_legend.String{i} = ['cos(',num2str(i),'x)'];
end
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Legend についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
