Plotting datapoint as you calculate them
5 次查看(过去 30 天)
显示 更早的评论
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 个评论
采纳的回答
Star Strider
2018-6-16
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 个评论
Star Strider
2018-6-17
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.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Annotations 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!