Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

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.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8757987
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T14:26:06+00:00 2026-06-13T14:26:06+00:00

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

  • 0

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',...')
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T14:26:07+00:00Added an answer on June 13, 2026 at 2:26 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose I have a Python class that I want to add an extra property
Suppose I want to put objects that identify a server into a stl set
suppose that I have made some changes since last update (revert) to my a.b
For instance, suppose I have a branch that I want to update with the
Ok another question in VHDL. Below is my code. Suppose that I want my
Actually i want that, suppose i have a colorcode #FF3366. How can i examine
Suppose I want to find the longest subsequence such that first half of subsequence
Suppose I want to write an app for Android OS that is not going
Suppose you have a module that you know is safe. You want to mark
Example: Suppose in the following example I want to match strings that do not

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.