Plotting datapoint as you calculate them
5 views (last 30 days)
Show older comments
Keith Elliott
on 16 Jun 2018
Commented: Star Strider
on 17 Jun 2018
I realise that it's easy enough to plot a simple graph of x and y data which have already been calculated in an array, eg :
x=-1:0.01:1;
y=sqrt(1-(x.^2));
plot(x,y)
will give me a chart of my simple curve.
However, if I change the increment from 0.01 to 1E-7 the calculations are fast enough but the plot takes quite a while to appear.
I'd like to plot the x,y position data as they're calculated rather than waiting until the end of all the calculations.
I tried this :
x=-1;
while(x<1)
y=sqrt(1-(x^2));
plot(x,y)
hold on
x=x+0.01
end
%note each iteration's datapoints (x&y) are deliberating not stored in an array as I won't need them.
but it only plots my last point rather than plotting all the points on the curve as it calculated them.
I'm guessing that Matlab only undertakes a plot once calculation loops are completed (?).
It might be that I'm being dumb or maybe that plotting a curve point by point as they are calculated just isn't supported?
I realise I can just use the first method, keep all my data pairs and plot at the end of all calculations. This will, in practice, probably result in me spending a lot of cumulative unnecessary time sitting in front of my computer!
Can anyone offer a solution to help with my preferred approach of plotting the data 'point by point' as they are calculated (or confirm that Matlab just doesn't do this sort of thing) please ?
0 Comments
Accepted Answer
Star Strider
on 16 Jun 2018
Try these:
figure(1)
x=-1;
while(x<1)
y=sqrt(1-(x^2));
plot(x,y,'p')
axis([0 2 0 1.5])
drawnow
x=x+0.01;
end
figure(2)
hold all
x=-1;
while(x<1)
y=sqrt(1-(x^2));
plot(x,y,'p')
axis([0 2 0 1.5])
drawnow
x=x+0.01;
end
If you have R2014b or later, also see the documentation on animatedline (link) and related functions.
Experiment to get the result you want.
6 Comments
Star Strider
on 17 Jun 2018
As always, my pleasure.
Writing a function to call to plot your data at the appropriate times would be my approach. The persistent (link) variable option could be helpful if you want to track progress, and have the function plot all previous data as well.
More Answers (0)
See Also
Categories
Find more on Annotations 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!