I run
loglog(x,y);
legend('First script');
from the first script.
Then, hold on;
.
I run a similar second script.
I see two graphs correctly, but only the initial legend.
I want to increment the legend from different scripts.
How can you add to the legend from a single script?
Another possible way to do this and it is called 'DisplayName':
hold all
for ind=1:3
x=[0:0.1:10];
plot(x, sin(x)+ind, 'DisplayName',['sin + ' num2str(ind)]);
end
legend(gca,'show')
Just use this property when plotting from wherever to whatever axes :)
There are even more things possible, like dynamic legend!
For more information see this: dynamic legend from undocumented matlab
EDIT
you have to update the legend after every run of a script. you can do this with the dynamic legend or by just legend('off'); legend('show')
The easiest way to deal with this is to save the handle to the legend when you create it, then whey you are ready to update the plot with a new legend with another series included, delete the legend and make a new one:
legendStrings = {'First script'};
h_legend = legend(legendStrings{:});
% ... computations, hold on and additional plot on axis
delete(h_legend);
legendStrings{end+1} = 'Second script';
h_legend = legend(legendStrings{:});
% rinse and repeat...
Usually with graphics objects, such as a textbox, I would say just reuse the object via the handle (don't delete
). However, if you update the legend instead of replacing it, you have to worry about more than just the strings. The MathWorks solution referenced by zroth actually seems to address this approach!
As an alternatively to delete
and create new, you can also toggle the legend on and off with legend('off'); legend('show');
as the answer in Eugenio's comment suggests.
The simplest way is:
hold all;
p1=plot(1:10,1:10);
legend(p1,'1'); % in this way plot(x,y) is saved as variable p1 with name '1'
p2=plot(1:10,11:20); % different data set
legend(p2,'2');
legend(gca,'off');
legend('show');
This is connection of few methods, it's very simple and it could be use everywhere.
I had a similar issue: I plotted three sets of experimental data first, then got into my parameter estimation to simulate the function and wanted to plot the model data every time, holding on to the experimental data but deleting the model data from the previous run. And I wanted the legend to show that. I was able to do it with a combination of solutions from different questions.
Initial commands (top of my main)
close all
will ensure your plot starts anew every time
First plot (in my main)
plot(points,expdata1,'ro','DisplayName','Experimental, L= 0.1 m'); hold on
plot(points,expdata2,'bo','DisplayName','Experimental, L= 0.2 m');
plot(points,expdata3,'go','DisplayName','Experimental, L= 0.3 m');
legend('-DynamicLegend','Location','Best')
drawnow
h_old=plot(0,250);
drawnow
forces the plot to be drawn right away, and h_old
is just a "placeholder" that I make use of later on. I chose 0,250 because it's in the range of the data (otherwise it messes up the axis)
Second Plot (in the called function)
h(1)=plot(points,modeldata1,'r-','DisplayName','Model Data, L= 0.1 m');
h(2)=plot(points,modeldata2,'b-','DisplayName','Model Data, L= 0.2 m');
h(3)=plot(points,modeldata3,'g-','DisplayName','Model Data, L= 0.3 m');
delete(h_old);
h_old = h;
drawnow
I delete h_old
and overwrite it with the new plots I created. That way, at the second iteration the plot from the 2nd iteration will be plotted, the plot from the 1st will be deleted and after these operations I get it to display the plot (again drawnow
).
plot(x,y);
legend('y');
hold on;
plot(x,z);
hold off;
% Now add a new item to the legend
h = legend();
newleg = h.String;
newleg{end+1} = 'z';
legend(newleg);