How to update plot data manually?

Is there a way to manipulate plot data without showing the updated plot information and then manually update the information all at once?
What I would like to do:
I have four plots and for each plot I manipulate the data and axes. The updates can be seen in the order they are perfromed. I would like to update the each individual axes and "draw" the updates all at once.
Clarification: I am working within a GUI designed in GUIDE. All plots are on the same figure.

Answers (1)

Adam Danz
Adam Danz on 22 May 2019
Edited: Adam Danz on 30 May 2019
What about giving refreshdata() a shot? Here's how it would work with your description.
% Create your 4 axes and their initial lines
x = -pi :.1: pi;
fh = figure();
subplot(2,2,1)
h1 = plot(x,sin(x));
subplot(2,2,2)
h2 = plot(x,2*sin(x));
subplot(2,2,3)
h3 = plot(x,cos(x));
subplot(2,2,4)
h4 = plot(x,2*cos(x));
% Setup data source
% Set up which variables will be used to define
% the (x,y) coordinates for each line object
set(h1, 'XDataSource', 'x', 'YDataSource', 'y1')
set(h2, 'XDataSource', 'x', 'YDataSource', 'y2')
set(h3, 'XDataSource', 'x', 'YDataSource', 'y3')
set(h4, 'XDataSource', 'x', 'YDataSource', 'y4')
% Change the plot data (axes won't update yet)
x = pi : 0.1 : 2*pi;
y1 = sin(x);
y2 = 2*sin(x);
y3 = cos(x);
y4 = 2*cos(x);
% Update all axes
refreshdata(fh, 'caller')
In my example I supplied the figure handle in refreshdata() but you can supply a vector of line objects ([h1,h2,h3,h4]) or axis handles.
Alternatively, you could use drawnow() at the end of the updating loop where you update the XData and YData properties.

7 Comments

I did not know about this. Unfortunatley I'm working within a GUI so I don't have access to the matlab workspace. I'll update my question.
So these plots don't update because you use the XDataSource property rather than manipulating XData directly?
Adam Danz
Adam Danz on 22 May 2019
Edited: Adam Danz on 22 May 2019
"Unfortunatley I'm working within a GUI so I don't have access to the matlab workspace"
Any change you make will have to be done to the GUI code itself. The solution I proposed would be implemented within the GUI code in the same way as if it were implemented outside of a GUI. You'd just need to specify the the caller workspace (I updated my answer to reflect that).
"So these plots don't update because you use the XDataSource property rather than manipulating XData directly?"
The advantage of using refreshdata() rather than updating the XData and YData is that all of your plots will be updated simultaneously. The X/YData would have to be updated for each plot one at a time.
Thank you! This is very helpful and I'm going to test it out now. I am also manually updating the axes limits, so I imagine those will be updated one at a time unless there's way to delay that as well.
One of my plots uses imagesc, which doesn't seem to have the XDataSource/YDataSource property so I'm guessing this only works for line plots? Still, very helpful.
Do you need to update the axis limits on each iteration? Would it work if they were on 'auto'?
xlim auto
ylim auto
Unfortunately imagesc() handle doesn't have the X/YDataSource property (but it will have X/Ydata).
I appended my answer again to suggest the use of drawnow() after updating x/ydata.
So in this GUI I have a "page" data structure which fills the plots with data and you can switch through multiple pages. So when switching pages I want to save the current zoom for that page. Setting to auto will work but it loses the capability to keep track of what the current zoom was for a given page.
What is the difference between drawnow and refreshdata?
Adam Danz
Adam Danz on 22 May 2019
Edited: Adam Danz on 29 May 2019
The descriptions in the links to the documentation I provided will answer that. drawnow() merely updates all plots if they haven't been updated already. This is sometimes necessary when creating a plot within a loop where you want the axes to update every iteration rather than at the end of the loop.
The refreshdata() command just updates the line objects that were setup using the X/YDatdaSource properties.

Sign in to comment.

Categories

Find more on Graphics Performance in Help Center and File Exchange

Asked:

on 22 May 2019

Edited:

on 30 May 2019

Community Treasure Hunt

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

Start Hunting!