why we should use plot properties like xdata over calling the plot method repeatedly in matlab

Using plot properties like xdata and ydata in Matlab provides a more efficient way of updating plot data compared to repeatedly calling the plot method.

When a plot is created using the plot method, Matlab creates an object for that plot, holding all of the plot information. If we want to update the plot with new data, we could simply call plot again with the new data. However, this creates a new set of plot objects, which consumes more resources.

Instead, we can use the set method to update individual plot properties, such as the xdata and ydata. This will update the existing plot object data, without creating a new one, saving resources.

For example, let's say we have some initial x and y data, and we create a plot using the plot method:

main.m
x = linspace(0,1,10);
y = sin(x);
plot(x, y);
46 chars
4 lines

Now we want to update the y data with some new values. We could call plot again, but instead we can update the existing plot object using the set method:

main.m
new_y = cos(x);
h = get(gca, 'Children');  % get handle to the plot object
set(h, 'YData', new_y);    % update ydata
117 chars
4 lines

This updates only the y data of the existing plot object, without creating a new one. This can be particularly useful when updating plots in real-time, where creating a new plot object every time step would not be practical.

Therefore, using plot properties like xdata over calling the plot method repeatedly is a more efficient way of modifying existing plots in Matlab.

related categories

gistlibby LogSnag