Is there any way to refresh a graph partly using 'drawnow'?

8 visualizaciones (últimos 30 días)
Said Kemal
Said Kemal el 26 de Mzo. de 2023
Respondida: Jack el 26 de Mzo. de 2023
I am designing an animation. I have many different object to draw. Some of them moves on the graph. That's why, they need to update in each iteration. Some of them is stationary. In this case, it is enough to draw them just once. Is there any way to use drawnow for refreshing entire animation except stationary ones. If not, what are the alternatives of drawnow?

Respuesta aceptada

Jack
Jack el 26 de Mzo. de 2023
Hi,
Yes, you can use the drawnow function to refresh the animation, but only update the objects that need to be updated. One way to achieve this is by keeping track of which objects are stationary and which ones are moving.
For example, you can create a list of moving objects and a list of stationary objects, and then update only the moving objects in each iteration:
moving_objects = [obj1, obj2, obj3]; % list of moving objects
stationary_objects = [obj4, obj5, obj6]; % list of stationary objects
while animation_is_running
% update moving objects
for i = 1:length(moving_objects)
update_object_position(moving_objects(i)); % update object position
draw_object(moving_objects(i)); % draw object
end
% refresh the animation
drawnow;
end
Alternatively, you can use the set function to update the properties of the moving objects directly, without redrawing the entire animation each time:
moving_objects = [obj1, obj2, obj3]; % list of moving objects
stationary_objects = [obj4, obj5, obj6]; % list of stationary objects
while animation_is_running
% update moving objects
for i = 1:length(moving_objects)
update_object_position(moving_objects(i)); % update object position
set(moving_objects(i), 'XData', new_xdata, 'YData', new_ydata); % update object data
end
% refresh the animation
drawnow;
end
This approach can be faster and more efficient, as it avoids redrawing the entire animation each time.

Más respuestas (0)

Categorías

Más información sobre Animation en Help Center y File Exchange.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by