Comet plot with slider
4 views (last 30 days)
Show older comments
Does anyone know if we can get the comet plot to work with a slider. So that we can go back and forth in the comet plot. This will help in visualizing how the plot takes shape with time or at any particular time.
Or does anyone know of a tool that helps us do this?
Thank you very much.
0 Comments
Answers (1)
Jaswanth
on 2 Aug 2024
Hi,
To create an interactive comet plot in MATLAB with a slider, you can use a combination of the comet function and a GUI slider to control the visualization. The comet function in MATLAB is typically used to create an animated plot, but you can manually update the plot based on the slider's value to achieve interactivity.
Please refer to following example of creating comet plot with slider:
function cometPlotWithSlider()
% Create the figure and axes
fig = figure('Position', [100, 100, 800, 600]);
ax = axes('Parent', fig, 'Position', [0.1, 0.3, 0.8, 0.6]);
% Generate data for the comet plot
t = linspace(0, 2*pi, 100);
x = cos(t);
y = sin(t);
% Create the slider
slider = uicontrol('Style', 'slider', 'Min', 1, 'Max', length(t), ...
'Value', 1, 'Position', [150, 50, 500, 20], ...
'Callback', @updateComet);
% Initial plot setup
hPlot = plot(ax, x(1), y(1), 'b-', 'LineWidth', 2);
hold on;
hHead = plot(ax, x(1), y(1), 'ro', 'MarkerFaceColor', 'r');
hold off;
axis equal;
% Update function for the slider
function updateComet(~, ~)
val = round(get(slider, 'Value'));
% Clear previous plot
cla(ax);
% Update the comet plot
comet(ax, x(1:val), y(1:val));
end
end
The updateComet function updates the plot based on the slider's value by clearing the previous plot and drawing the comet plot up to the current slider value.
Following is the figure created from the above example code through which we can slide through the comet plot.
I hope the solution provided above is helpful.
0 Comments
See Also
Categories
Find more on Contour Plots 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!