Suppose that I want to update a plot with a new data. What method should I choose?
- Set the
XDataSourceproperty to some name, update the variable, and callrefreshdata - Erase the original
plot, and callplotcommand again. - Use
Set('Xdata',...')
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Short answer : always use
Set('Xdata',...').Example code:
Long answer:
There are three relevant measures by which one should choose the best method.
Now, let’s analyze the possible methods.
Method(1) – refreshdata
M-lint immediately issues a warning in the line
y=sin(x.^3)Why does it happen?
refreshdatausesevalandm-lintcannot know that you will usey. Someone reading your code, might as well remove this line completely. This happened because you broke the encapsulation principle.refreshdataaccesses 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 wrotey = 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
refreshdatasource code, you will notice two ugly for-loops, that go through all of the graphics handles variables in your space. Here is the first: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
evalseveral 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
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,
XDataandYData. And that is exactly what you do. Also, the code runs really fast, as you can see from the comparison below.Since the new graphics engine hg2 (R2014b and up), you can also use property syntax for specifying data if you prefer that notation:
Conclusion : Easy to understand, easy to refactor, fast runtime
Here is the time comparison code
And the results: