Clear Filters
Clear Filters

How to add slider to animatedline plot?

52 views (last 30 days)
%% 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)
set(variable,'Xdata',x1,'Ydata',y1);
.
(few more x and y datas)
addpoints(g1,xc(q),yc1(q),zh(q)
addpoints(k1,zh(q),xc(q))
.
.
(few more addpoints statements)
end

Answers (1)

Yash
Yash on 4 Sep 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
end
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 Comment
Kushal Bollempalli
Kushal Bollempalli on 6 Sep 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)

Sign in to comment.

Categories

Find more on Animation in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!