MATLAB: How to continuously update an existing plot

MATLABplotplotting

Hi
I have a program in which user can choose any or multiple versions of an activity to run one after another. Each version is run over 5 datasets. 5 plots will be generated for each dataset. So I will have a total of 25 plots for each version. If I run all three versions, then a total of 5*5*3 plots will be generated which will become messy. So instead of generating all the plots, I want to merge the corresponding plot of each version for a particular dataset i.e. plot #1 of dataset#1 version#1 should be merged with plot#1 of dataset#1 version#1 and so on. How do I do this without opening and closing the plot?
Thanks
Nadia

Best Answer

Instead of creating lots of figures, the idea is to create one plot and alter its properties. This could mean altering its data points, or axes, or orientation, or whatever you require.
Create just one figure (or a very small number), and always keep track of the graphics handles of everything that you plot. All graphics functions return a handle (well, most of them), and you can use this handle to specify where to plot your data, to delete, or alter parts of a figure, axes, line, patch, text, or any other graphics object.
Create the figure once, and then do not close it: simply change it as required, and ditto for its contents: add or delete axes, lines, patches, etc.
For example, to plot a simple function:
>> fgh = figure(); % create a figure
>> axh = axes('Parent',fgh); % create axes
>> X = 0:0.1:pi;
>> Y = sin(X);
>> lnh = plot(axh,X,Y); % plot in those axes
Now you can use set and get to alter properties of the objects (or dot notation of using R2014b or more recent). Note that you will need to carefully read the list of object properties (e.g. line properties), and be aware that sometimes multiple properties may need to be changed to get the desired effect:
>> set(lnh,'XData',[0,1,2],'YData',[1,2,1]) % change the line data
You can delete or change any graphics objects:
>> cla(axh) % clear those axes
Now that the axes are clear simply plot some new data... and so on.
To get the best information read the MATLAB documentation: