Saving plots in For loop

13 次查看(过去 30 天)
When I am using the below command, it is giving me error as invalid or missing path.
saveas(gcf,['Monthly plot for: ', datestr(Yr_Avg.dd(j)), '.png'])

采纳的回答

Siraj
Siraj 2022-7-4
编辑:Siraj 2022-7-4
Hii,
It is my understanding that you are getting the mentioned error (error1.PNG) beacuse your are including the special character ":" (colon) in your file name, which is not allowed. If you remove the colon and just leave a blank space after "for", the code will work fine.
I am attaching the 2 valid ways of using "saveas()".
Refer to the documentation of saveas for more clarity.
Hope it helps.
curr_fig1 = figure();
x = linspace(-2*pi,2*pi,100);
y = sin(x);
figure(1);
plot(x,y);
t = datetime(2022,11,30);
% saveas(curr_fig1,strcat("Monthly plot for ", datestr(t)), "png") % First Way
saveas(curr_fig1,['Monthly plot for ', datestr(t), '.png']) % Second Way
  1 个评论
Harjas
Harjas 2022-7-4
Thanks. It worked. Suppose I have 'x' plots. I want to create subplots of 6 plots in one figure and save it, Then, then take next 6 plots and save them ....... so on till all plots are saved . Do you know how can I do that? I tried to store all the graphics in one variable h. Will it be wise to then use for loop to create subplots or do you know any other way ?

请先登录,再进行评论。

更多回答(2 个)

Voss
Voss 2022-7-4
I think it's because you can't have a colon (:) in a file name.

Image Analyst
Image Analyst 2022-7-4
Use exportgraphics and no colon in the filename because that is a drive letter indicator.
baseFileName = sprintf('Monthly plot for %d.png', datestr(Yr_Avg.dd(j))); % Don't use banned characters like colons and slashes.
folder = pwd; % Wherever you want.
fullFileName = fullfile(folder, baseFileName);
fprintf('Saving plot : %s\b', fullFileName);
exportgraphics(gca, fullFileName); % Save current graph. Or use gcf if you want the entire figure.
  2 个评论
Harjas
Harjas 2022-7-4
Thanks. Suppose I have 'x' plots. I want to create subplots of 6 plots in one figure and save it, Then, then take next 6 plots and save them ....... so on till all plots are saved . Do you know how can I do that? I tried to store all the graphics in one variable h. Will it be wise to then use for loop to create subplots or do you know any other way ?
Image Analyst
Image Analyst 2022-7-4
for k = 1 : numFigs
hFig = figure; % Create a new figure.
for k2 = 1 : 6
subplot(3, 2, k2);
% make your graphs
end
% Now all 6 graphs have been made on a new figure so save
% the entire figure of 6 graphs as one single image.
baseFileName = sprintf('Monthly plot for %d #%d.png', datestr(Yr_Avg.dd(j)), k); % Don't use banned characters like colons and slashes.
folder = pwd; % Wherever you want.
fullFileName = fullfile(folder, baseFileName);
fprintf('Saving plot : %s\b', fullFileName);
exportgraphics(hFig, fullFileName); % Save current graph. Or use gcf if you want the entire figure.
% Close down this figure after it's been saved.
close(hFig);
end

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 2-D and 3-D Plots 的更多信息

产品


版本

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by