Matlab real time chart

I am new to Matlab and I want to display some data in real time. My approach:

figure; hold on; for i = 1:1000; plot(i, i); drawnow; end 

But it has poor performance.

I also found a suggestion here about stackoverflow: https://stackoverflow.com/a/166263/167123 ... but only the last given data is drawn, so I always see only one point in the picture.

+6
source share
2 answers

Instead of making a high level plot call, consider setting the properties of the descriptor, namely XData and YData , in a loop:

 figure(1); lHandle = line(nan, nan); %# Generate a blank line and return the line handle for i = 1:1000 X = get(lHandle, 'XData'); Y = get(lHandle, 'YData'); X = [X i]; Y = [Y i]; set(lHandle, 'XData', X, 'YData', Y); end 

Performing this, a tic / toc before / after the code gives 0.09 seconds; naive plot , as you, as you saw, gives a runtime of almost 20 seconds.

Note that in this example, I used get to generate a dataset; I assume that for a real-time plot, you have some DatasetX and DatasetY to plot, so you will need to work with your data. But in the end, as soon as you have the data set that you want to build at a specific time, just set the entire string to XData and YData .

Finally, note that this set call becomes a bit cumbersome for very large datasets, since we must set the line data each time, not add to it. (But this is certainly even faster than using plot .) This can be quite good, depending on how often your dataset changes. See this question for more details.


EDIT . With MATLAB R2014b, an animinatedline object simplifies the mapping of points from streaming data:

Animated line objects optimize line animation by accumulating data from a stream data source. After you create an initial animated line using the animated line function, you can add new points to the line without overriding existing points. Change the appearance of the animated string by setting its properties.

+20
source

To improve the preliminary work, you should use clf, it clears the figure. Otherwise, all charts are stacked on top of each other.

If you want to make plot(x,y) in real time, you must do:

 figure; hold on; for i = 1:1000; clf; plot(x(1:i), y(1:i)); drawnow; end 
0
source

Source: https://habr.com/ru/post/902441/


All Articles