How to add slider to animatedline plot?

44 visualizaciones (últimos 30 días)
Kushal Bollempalli
Kushal Bollempalli el 1 de Sept. de 2023
Comentada: Kushal Bollempalli el 6 de Sept. de 2023
%% I want to add slider and control the animated plot. I would like to control the plot with slider such as if i bring my slider back, the plot to rewind and stop at given point. My code is
g = animatedline(ax2,'Color','k'); % animateed line for hypocycloid
g1 = animatedline(ax2,'Color','b');
g2 = animatedline(ax2,'Color','m');
k1 = animatedline (ax1,'Color','r');
for q= 1:length(t)
(few equations)
(few more x and y datas)
(few more addpoints statements)

Respuestas (1)

Yash el 4 de Sept. de 2023
To control an animated plot using a slider in MATLAB, you can use the "uifigure" , "uislider" and related UI components.
For more information on "uifigure" and "uislider" refer to the documentation below:
Here's a basic example of how to add a slider to control the animation of your plot:
% Create a figure and axes
fig = uifigure('Name', 'Animated Plot');
ax1 = uiaxes(fig, 'Position', [50, 150, 400, 300]);
% Create a slider
slider = uislider(fig, 'Position', [50, 100, 400, 3]);
% Set the limits based on your data
slider.Limits = [1 length(t)];
% Initialize the slider at the start position
slider.Value = 1;
% Create an empty animated line
g = animatedline(ax1, 'Color', 'k');
% Callback function for the slider
slider.ValueChangedFcn = @(src, event) updatePlot(src, g, x, y);
% Function to update the plot based on the slider value
function updatePlot(slider, line, x, y)
t_index = round(slider.Value); % Get the value of the slider
% Update your plot using the selected index (t_index)
% Example:
addpoints(line, x(t_index), y(t_index)); % Add new points
drawnow; % Redraw the plot
In this example:
  1. We create a UI figure and a UI slider.
  2. We set the slider limits based on the length of your data (t).
  3. We create an animated line g on the axes.
  4. We define a callback function "updatePlot" for the slider's "ValueChangedFcn". This function updates the plot based on the slider's value.
  5. In the "updatePlot" function, you can update your animated line (line) with data from your calculations, using the selected index from the slider.
You will need to adapt the "updatePlot" function to update your animated lines (g1, k1, etc.) with the relevant data points for your animation. Also, make sure to customize the positions and properties of the UI components to fit your layout and design requirements.
This basic example should provide you with the foundation for controlling your animated plot with a slider.
I hope this helps.
  1 comentario
Kushal Bollempalli
Kushal Bollempalli el 6 de Sept. de 2023
Hi yash,
Thank you for the answer. I do have few doubts with the answer. What does line and t_index mean in the function updatePlot?
I have more than 20 addpoints in my code. Should I write all these again in the function?
Moreover, when i am using uifigure, it is creating new figure. (For instance, my circles are in figure 1 and the animation comes on figure 2)

Iniciar sesión para comentar.


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