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

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

 Réponse acceptée

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 commentaires

Thanks a lot though , but is there any way such that plot and legends appear simultaneously.in your above syntex whole plot will appear then after that all legends will come.
The plots and legends will appear at the same time in the above unless you are single-stepping through in the debugger. plots and legends are not rendered to the display until you have a pause() or drawnow() or figure() or uiwait() or waitfor(), or the keyboard regains control.
Is there a way to hide certain entries in the for loop? I have a case where only certain entries should have a legend entry, and I don't want the others to show up in the legend.
Hello Walter,
I have several .mat files that I am processing in a for loop. I want to pass the name of a parameter and its value in my legend. So if I have 5 .mat files to process I want my legend to give me:
ALT 204
ALT 205
ALT 210
ALT 200
ALT 209
How can I do this. Please help.
Thank you.
Given any particular file, how do you know what the name of the parameter and the associated value is? Can you determine it from the name of the file? Can you determine it by the names of the variables stored in the file? Are you wanting to specify a list of parameter names and values ahead of times, and those can be used to predict which files to load? Are you wanting to specify a list of parameter names and values ahead of times, and those can be used to predict which variable names to extract from the file?
Given a particular variable name loaded from the .mat file, what are your x (independent variable) and y (dependent variable) to plot?
Sara Fawal
Sara Fawal le 2 Juil 2020
Modifié(e) : Walter Roberson le 2 Juil 2020
Hello Water,
Basically I am doing the following:
filename = 'TR_LIST.xlsx';
[num,txt,raw] = xlsread(filename);
DATA1 = [];
DATA2 = [];
for i=1:numel(txt)
load (txt{i})
Extraction of data and calculation from the matlab structure is here.....
Now to plotting....
DATA1(i) = fix(ALT); Saves all the Altitudes values
DATA2(i) = MACH; Saves all the Mach numbers
figure(1)
plot(THRN(IND(1):IND(end)),NGN(IND(1):IND(end)))
hold all
xlabel('Corrected Gas Generator Speed')
ylabel('Corrected Thrust')
legend(strcat('ALT', {' '}, num2str(DATA1(i)), {' '}, 'm', {' '}, 'MACH', {' '}, num2str(DATA2(i))))
My issue is the legend I am not sure how to get ALL the values of Altitude and Mach number to appear with the corresponding line on the plot, however many there are.
Thank you again Walter :O)
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
Thank you very much Walter :O)

Connectez-vous pour commenter.

Plus de réponses (1)

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!

Translated by