Saving plots in For loop

1 visualización (últimos 30 días)
Harjas
Harjas el 4 de Jul. de 2022
Comentada: Image Analyst el 4 de Jul. de 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'])

Respuesta aceptada

Siraj
Siraj el 4 de Jul. de 2022
Editada: Siraj el 4 de Jul. de 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 comentario
Harjas
Harjas el 4 de Jul. de 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 ?

Iniciar sesión para comentar.

Más respuestas (2)

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

Image Analyst
Image Analyst el 4 de Jul. de 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 comentarios
Harjas
Harjas el 4 de Jul. de 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 el 4 de Jul. de 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

Iniciar sesión para comentar.

Categorías

Más información sobre Printing and Saving en Help Center y File Exchange.

Productos


Versión

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by