Saving plots in For loop

1 view (last 30 days)
Harjas
Harjas on 4 Jul 2022
Commented: Image Analyst on 4 Jul 2022
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'])

Accepted Answer

Siraj
Siraj on 4 Jul 2022
Edited: Siraj on 4 Jul 2022
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 Comment
Harjas
Harjas on 4 Jul 2022
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 ?

Sign in to comment.

More Answers (2)

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

Image Analyst
Image Analyst on 4 Jul 2022
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 Comments
Harjas
Harjas on 4 Jul 2022
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 on 4 Jul 2022
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

Sign in to comment.

Categories

Find more on Printing and Saving in Help Center and File Exchange

Products


Release

R2018a

Community Treasure Hunt

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

Start Hunting!