How to continuously update an existing plot?

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

Accepted Answer

Stephen23
Stephen23 on 27 Dec 2016
Edited: Stephen23 on 27 Dec 2016
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:
  2 Comments
Nadia A
Nadia A on 27 Dec 2016
Edited: Nadia A on 27 Dec 2016
Thank You Stephen.. Each dataset will have 5 plots and each plot will have plots of 3 versions in it. How do I save all the handles? Can it be assigned to an array of structures?
In my program, first all the datasets for one version will be executed followed by other versions.
Stephen23
Stephen23 on 27 Dec 2016
@Nadia A: how you save handles depends on the MATLAB version. Prior to 2014b handles were numeric values, so they could be saved in simple double arrays. Since 2014b they are objects which need to be saved. At this point you can refer to the links that I gave you: open this page and scan down the page:
and you will find the text "...preallocating, and object arrays". Click the link to open this:
"Use the gobjects function to preallocate arrays for graphics objects."

Sign in to comment.

More Answers (0)

Categories

Find more on Graphics Object Programming in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!