How to generate graph at the same time that data is obtained?

Answers (1)

One thing you can do is add new data to an already existing plot.
figure;
plot(1:10,1:10);
ah = gca;
for loopIX = 11:100
%New Data becomes available
newXValue = loopIX;
newYValue = rand;
%Add the new data to the plot
ah.Children.XData(end+1) = newXValue;
ah.Children.YData(end+1) = newYValue;
end
Note that you may get warnings about the data arrays being sized wrong between the Xdata update and the Ydata update.

5 Comments

To avoid that warning, use
set(ah.Children, 'XData', [ah.Children.XData, newXValue], 'YData', [ah.Children.YData, newYValue])
Better yet, use animatedLine() to handle all of this.
That requires going back to the old set notation! Not a bad thing, but it doesn't feel as clean.
I hadn't known about the animatedLine/addpoints functionality. For reference, here's an example from the documentation of how that could be used.
h = animatedline('Marker','o');
x = 1:5;
y = 1:5;
addpoints(h,x,y)
x = 6:10;
y = 6:10;
addpoints(h,x,y)
What I need is to see the graph being plotted at each iteration. And not the figure with all the points already computed. To facilitate visualization of wave propagation.
alcides silva:
At each time that you have a new point available, call addpoints() on the animatedLine() object, and then call drawnow().
Depending on your needs, you might want to set the 'MaximumNumPoints' option when you create the animatedLine
What I was hoping to do, I got with drawnow update or drawnow expose. Great. Thank you so much.

Sign in to comment.

Categories

Find more on Graphics Performance in Help Center and File Exchange

Products

Tags

Asked:

on 20 Dec 2017

Commented:

on 21 Dec 2017

Community Treasure Hunt

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

Start Hunting!