Plotting datapoint as you calculate them

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

Accepted Answer

Star Strider
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
Keith Elliott
Keith Elliott on 17 Jun 2018
Thanks. I can output data to a file (fopen & fprintf) for postprocessing easily enough - it seems to take about 1min per GB of data (on my lower powered machine) which is fine for what I want.
Your highlighting of the 'drawnow' function has been hugely helpful in ascertaining Matlab's value to me : While I want to calculate positions many times, I don't need to see all the data as it's produced (I just want to see the general form as it progresses so that I can terminate a run early if required). It's a simple matter just to plot every 10,000th data pair (or whatever frequency I would like)
So now I don't have 'deal-breaking' issues with Matlab's processing speed, mathematical functionality or flexibility in managing graphical output and I can easily output all data to a file if I want.
I suspect the Matlab parallel computing toolbox might also help if I do run into processing speed issues later.
I can see why Matlab is popular : as a development tool it has pretty good speed and flexibility.
Thanks again for your help.
Star Strider
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.

Sign in to comment.

More Answers (0)

Tags

Products


Release

R2018a

Community Treasure Hunt

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

Start Hunting!