Video for stacked plot

4 views (last 30 days)
Bhargav
Bhargav on 13 Mar 2024
Commented: Bhargav on 9 Jul 2024
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 ?

Answers (1)

Shubham
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)
data = 1500x4 table
data1 data2 data3 data4 ________ ________ ________ _____ 0 0 0 1 0.020956 0.041904 0.010479 2 0.041904 0.083734 0.020956 3 0.062832 0.12542 0.031432 4 0.083734 0.16688 0.041904 5 0.1046 0.20805 0.052371 6 0.12542 0.24885 0.062832 7 0.14618 0.28922 0.073287 8 0.16688 0.32908 0.083734 9 0.1875 0.36836 0.094171 10 0.20805 0.40699 0.1046 11 0.2285 0.44491 0.11501 12 0.24885 0.48205 0.12542 13 0.26909 0.51834 0.13581 14 0.28922 0.55372 0.14618 15 0.30922 0.58812 0.15654 16
% 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
Shubham
Shubham on 5 Jun 2024
Hey @Bhargav, did you find the above answer helpful? If so, could you mark the answer as accepted?
Bhargav
Bhargav on 9 Jul 2024
you can mark it as accepted, never got to it. It was a nice add on. will give it a try later Thank you though

Sign in to comment.

Products


Release

R2023b

Community Treasure Hunt

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

Start Hunting!