Plot with lagging

2 views (last 30 days)
Yan Mord
Yan Mord on 26 Mar 2012
Answered: Ayush on 4 Dec 2024
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,

Answers (1)

Ayush
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:
  • 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.
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:

Categories

Find more on Audio I/O and Waveform Generation in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!