I run
loglog(x,y);
legend(\'First script\');
from the first script.
Then, hold on;
.
I run a similar second script.
I see two g
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
).