Video for stacked plot
3 views (last 30 days)
Show older comments
Is there a function which seeks through the stacked plot as a video. I have a 15000 data points,
s=stackedplot(data)
Can we save this stacked plot as a video ?
0 Comments
Answers (1)
Shubham
on 20 Mar 2024
Hey Bhargav,
For converting a stacked plot as a video, you can use "VideoWriter" object to capture frames and write them into a video file. You can first think of the size of data you wish to show in a single frame and a step size for shifting the data (so that consequent frames in a video do not have abrupt changes). Please have a look at the following code snippet for saving a video from a stacked plot using some dummy data:
% Data Generation
numDataPoints = 1500;
t = linspace(0, 10*pi, numDataPoints); % Time vector
x = 1:numDataPoints; % for xticks only
% Generating sine waves data for plotting stackedplot
data = [sin(t)' sin(2*t)' sin(0.5*t)' x']; % 3 variables with different frequencies and Xticks
data = array2table(data)
% display full stackedplot
figure(2);
stackedplot(data,'XVariable','data4');
windowSize = 150; % Number of data points to display in each frame
stepSize = 30; % Number of data points to move the window for each frame
% Prepare the video file
videoName = 'stackedplot_sine_wave_video.mp4';
v = VideoWriter(videoName, 'MPEG-4');
v.FrameRate = 10; % Set frame rate for smooth video
open(v);
% Generate and capture frames
for startIdx = 1:stepSize:(numDataPoints-windowSize+1)
endIdx = startIdx + windowSize - 1;
% Extract the subset of data for the current window
dataSubset = data(startIdx:endIdx, :);
% Create the stacked plot for the current window
figure('Visible', 'off');
s = stackedplot(dataSubset,'XVariable','data4'); % Assigning data4 as Xvariable (else the xaxis would be static in video)
drawnow;
% Capture the frame
frame = getframe(gcf);
writeVideo(v, frame);
close(gcf);
end
close(v);
disp(['Video saved as ', videoName]);
% play the saved video
implay('stackedplot_sine_wave_video.mp4')
Attaching a random frame of the produced stacked plot for your reference:
I have currently set the frame rate to 10. I am taking a subset of the data, plotting it and saving the video frame by frame using "VideoWriter" object. You can play the video in MATLAB using "implay()" function. You can also find the produced video as a zip attachment.
I hope this helps!
2 Comments
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!