- Have your “x” and “y” data ready.
- Use “VideoWriter” function to create an .avi file.
- Loop through your data points, updating the plot for each point with the specified time lag.
- Capture each frame using “getframe” and write it to the video file.
Plot with lagging
1 view (last 30 days)
Show older comments
Hi, I want to plot series of data (y vs. x) with lagging of time t (33.33mS) between each point and then to save that as a video (.avi format). Any assistance will be appreciated, Regards,
0 Comments
Answers (1)
Ayush
on 4 Dec 2024
Hi,
To create a video from a series of data points with a specific time lag between each point in MATLAB, you can follow these steps:
Refer to an example code below for a better understanding:
% Sample data
x = 1:100; % Example x data
y = sin(x); % Example y data
% Time lag in seconds
time_lag = 0.03333; % 33.33 milliseconds
% Set up the video writer
videoFileName = 'data_plot.avi';
v = VideoWriter(videoFileName);
v.FrameRate = 1 / time_lag; % Set frame rate based on time lag
open(v);
% Create a figure for plotting
figure;
hold on;
grid on;
xlabel('X-axis');
ylabel('Y-axis');
title('Data Plot with Time Lag');
% Plot each point with the specified time lag
for i = 1:length(x)
plot(x(i), y(i), 'bo'); % Plot the current point
drawnow; % Update the plot
% Capture the frame
frame = getframe(gcf);
writeVideo(v, frame);
% Pause for the specified time lag
pause(time_lag);
end
% Close the video writer
close(v);
disp(['Video saved as ', videoFileName]);
For more information on the “VideoWriter” and “getframe” functions refer to the below documentations:
0 Comments
See Also
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!