Plotting datapoint as you calculate them

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 ?

 Risposta accettata

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 Commenti

Many thanks!
As always, my pleasure!
I have a supplementary which you might be able to help with. Typically processes have a bottleneck at some point. When (/if) I get round to modelling 20+ objects interacting for, say, 100million iterations, there's going to be a lot of data produced. Even with comparatively small amounts of data (eg 5000 x,y coordinate pairs) Matlab's plot function is pretty painfully slow. I was wondering if the bottleneck is more likely to be in the Matlab functions or in my graphics processor (integrated graphics rather than a plug-in gpu ) and whether folk typically just output data to a file (from Matlab) and post process another way.
I’ll do my best. As I understand, MATLAB compiles to subroutine calls, some of which are implemented in machine code and some interpreted. The slowness is likely not your graphics processor. You would likely have to compile your code into machine language if you want to do real-time processing.
I would save your data to a file, and process it offline rather than in real time. You may want to contact MathWorks Sales to find out if any Toolboxes (some of which may be able to do real-time processing) meet your needs. I don’t do real-time processing now, so I have no experience with them.
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.
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.

Accedi per commentare.

Più risposte (0)

Prodotti

Release

R2018a

Tag

Community Treasure Hunt

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

Start Hunting!

Translated by