Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How should I update the data of a plot in Matlab?

Suppose that I want to update a plot with a new data. What method should I choose?

  1. Set the XDataSource property to some name, update the variable, and call refreshdata
  2. Erase the original plot, and call plot command again.
  3. Use Set('Xdata',...')
like image 819
Andrey Rubshtein Avatar asked Oct 27 '12 17:10

Andrey Rubshtein


People also ask

How do I edit a plot in MATLAB?

Choose the Edit Plot option on the figure window Tools menu. Click on the selection button in the figure window toolbar. Choose an option from the Edit or Insert menu. For example, if you choose the Axes Properties option on the Edit menu, MATLAB activates plot edit mode and the axes appear selected.

How do you improve the quality of a plot in MATLAB?

To get a high-resolution image from MATLAB, you may use the "copy figure" option. In your Matlab figure, go to the "Edit" option, select "Copy Figure", then paste it in MS word file using "crtl+v". If you want to use the figure in latex, then save the figure in the ". eps" format.

How do you fill a plot area in MATLAB?

area( X , Y ) plots the values in Y against the x-coordinates X . The function then fills the areas between the curves based on the shape of Y : If Y is a vector, the plot contains one curve. area fills the area between the curve and the horizontal axis.

What is Drawnow in MATLAB?

drawnow updates figures and processes any pending callbacks. Use this command if you modify graphics objects and want to see the updates on the screen immediately. example. drawnow limitrate limits the number of updates to 20 frames per second.


1 Answers

Short answer : always use Set('Xdata',...').

Example code:

function PlotUpdate()        x = 0:.1:8;     y = sin(x);     h = plot(x,y);     y = sin(x.^3);         set(h,'XData',x,'YData',y); end 

Long answer:

There are three relevant measures by which one should choose the best method.

  1. Code clarity - How easy it is for someone to read your code?
  2. Runtime - How quick each method performs its task?
  3. Code portability - How fast can you re-factor your code?

Now, let's analyze the possible methods.

Method(1) - refreshdata

function PlotUpdate()        x = 0:.1:8;     y = sin(x);     h = plot(x,y);     set(h,'YDataSource','y')     set(h,'XDataSource','x')     y = sin(x.^3);     refreshdata(h,'caller'); end 

M-lint immediately issues a warning in the line y=sin(x.^3)

The value assigned to variable `y` might be unused 

Why does it happen? refreshdata uses eval and m-lint cannot know that you will use y. Someone reading your code, might as well remove this line completely. This happened because you broke the encapsulation principle. refreshdata accesses variables from the caller workspace. Another way to take a look at this, suppose that you pass the handle of the plot to another function. The reader has no clue to why on earth you wrote y = sin(x.^3);, and how is it going to be related to the update of the plot.

Now let's discuss speed/runtime. By taking a look at refreshdata source code, you will notice two ugly for-loops, that go through all of the graphics handles variables in your space. Here is the first:

% gather up all the objects to refresh objs = {}; for k = 1:length(h)   obj = h(k);   objfields = fields(obj);   for k2 = 1:length(objfields)     % search for properties ending in DataSource     if strncmpi(fliplr(objfields{k2}),'ecruoSataD',10)       objs = {objs{:},obj, objfields{k2}};     end   end end 

Imagine that you have not one plot, but 100 plot and you want to update only the first. This will be very slow, because for each of the plots, you attempt to find the one you need! (I am leaving as an exercise for the reader to figure out what is ecruoSataD, and how it is used.)

Even if you give the relevant plot as an argument, you still have the second loop, that runs eval several times. Not exactly efficient. I will show a time comparison in the end.

Conclusion : Hard to understand, hard to refactor, slow runtime


Method (2) - Delete and re-plot

function PlotUpdate()        x = 0:.1:8;     y = sin(x);     h = plot(x,y);     set(h,'YDataSource','y')     set(h,'XDataSource','x')     y = sin(x.^3);               delete(h);     h = plot(x,y);     end 

This method is quite clear for the reader. You deleted the plot, and drew a new one. However, as we will see from the time comparison in the end, that is the slowest method.

Conclusion : Easy to understand, easy to refactor, very slow runtime


Method(3) - set('XData',...,'YData')

The code is really clear. You want to modify a two properties of your plot, XData and YData. And that is exactly what you do. Also, the code runs really fast, as you can see from the comparison below.

function PlotUpdate()        x = 0:.1:8;     y = sin(x);     h = plot(x,y);     y = sin(x.^3);               set(h,'XData',x,'YData',y); end 

Since the new graphics engine hg2 (R2014b and up), you can also use property syntax for specifying data if you prefer that notation:

function PlotUpdate()        x = 0:.1:8;     y = sin(x);     h = plot(x,y);     y = sin(x.^3);               h.XData = x;     h.YData = y; end 

Conclusion : Easy to understand, easy to refactor, fast runtime


Here is the time comparison code

function PlotUpdateTimeCompare()         x = 0:.1:8;     y = sin(x);     h = plot(x,y);     set(h,'YDataSource','y')     set(h,'XDataSource','x')     y = sin(x.^3);       tic     for i=1:100         refreshdata(h,'caller');     end     toc       tic     for i=1:100         delete(h);         h = plot(x,y);     end     toc           tic     for i=1:100         set(h,'XData',x,'YData',y);     end     toc   end 

And the results:

Elapsed time is 0.075515 seconds.
Elapsed time is 0.179954 seconds.
Elapsed time is 0.002820 seconds.

like image 73
Andrey Rubshtein Avatar answered Oct 12 '22 03:10

Andrey Rubshtein