Video seek for Stackedplot

1 view (last 30 days)
Bhargav
Bhargav on 13 Mar 2024
Answered: Shubham on 21 Mar 2024
Hi,
Is there MATLAB functional which helps us to seek through the stackedplot graph.
stackedplot(data)
set(gcf, 'Position', get(0,'Screensize')); % Maximize figure
set(findall(gcf,'-property','FontSize'),'FontSize',10); % Adjust font size
fig_name = fullfile(parent_folder, sprintf('%s_Figure.jpg', folder_name));
saveas(gcf, fig_name, 'jpeg');
% Create a VideoWriter object for the current subfolder
video_name = fullfile(parent_folder, [folder_name '_video.mp4']);
v = VideoWriter(video_name, 'MPEG-4');
open(v);
% Capture the frame and add it to the video
frame = getframe(gcf);
writeVideo(v, frame);
% Close the VideoWriter object
close(v);
close(gcf); % Close the figure
running this, the code gets out of the function with out stating an error
Error using parentdirectory
Expected input to be a non-missing string scalar or character vector.
Error in untitled11 (line 9)
parentdirectory(parent_folder_path);
Thanks,
Bhargav

Answers (1)

Shubham
Shubham on 21 Mar 2024
Hey Bhargav,
The error message that you are encountering suggests that the input requires to be a string. You may find the following MATLAB Answer relevant to your case:
Also, for saving a file in parent directory, you can refer to the following MATLAB Answer: https://www.mathworks.com/matlabcentral/answers/175881-save-folder-one-above-current-directory
For creating a video from stackedplot, you can refer to your previous thread: https://www.mathworks.com/matlabcentral/answers/2094016-video-for-stacked-plot?s_tid=prof_contriblnk
I have modified your code snippet as follows:
% Data Generation
numDataPoints = 1500;
t = linspace(0, 10*pi, numDataPoints); % Time vector
x = 1:numDataPoints; % for xticks only
% Generating data for plotting stackedplot
data = [sin(t)' sin(2*t)' sin(0.5*t)' x'];
data = array2table(data)
data = 1500×4 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
stackedplot(data,'XVariable','data4')
set(gcf, 'Position', get(0,'Screensize')); % Maximize figure
set(findall(gcf,'-property','FontSize'),'FontSize',10); % Adjust font size
% Use the current working directory as the parent folder
parent_folder = cd;
parent_folder = fullfile(parent_folder, '..');
folder_name = 'stacked_plot'; % Example folder name
% Construct the full path to the folder where the figure and video will be saved
full_path = fullfile(parent_folder, folder_name);
% Check if the folder exists, if not, create it
if ~exist(full_path, 'dir')
mkdir(full_path);
end
% Define the full file path for the figure and video
fig_name = fullfile(full_path, sprintf('%s_Figure.jpg', folder_name));
video_name = fullfile(full_path, [folder_name '_video.mp4']);
% Save the figure
saveas(gcf, fig_name, 'jpeg');
% Create a VideoWriter object for the video
v = VideoWriter(video_name, 'MPEG-4');
open(v);
% Capture the frame and add it to the video
frame = getframe(gcf);
writeVideo(v, frame);
% Close the VideoWriter object
close(v);
close(gcf); % Close the figure
You may also share your complete code.
I hope this helps!

Categories

Find more on 2-D and 3-D Plots in Help Center and File Exchange

Products


Release

R2023b

Community Treasure Hunt

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

Start Hunting!