How can i add legend in a for loop after each plot ?

239 views (last 30 days)
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

Accepted Answer

Walter Roberson
Walter Roberson on 11 Jun 2017
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 Comments
Walter Roberson
Walter Roberson on 2 Jul 2020
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

Sign in to comment.

More Answers (1)

Fabio Abbà
Fabio Abbà on 4 May 2023
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

Tags

Community Treasure Hunt

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

Start Hunting!