Saving plots in For loop

18 vues (au cours des 30 derniers jours)
Harjas
Harjas le 4 Juil 2022
Commenté : Image Analyst le 4 Juil 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'])

Réponse acceptée

Siraj
Siraj le 4 Juil 2022
Modifié(e) : Siraj le 4 Juil 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 commentaire
Harjas
Harjas le 4 Juil 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 ?

Connectez-vous pour commenter.

Plus de réponses (2)

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

Image Analyst
Image Analyst le 4 Juil 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 commentaires
Harjas
Harjas le 4 Juil 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 le 4 Juil 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

Connectez-vous pour commenter.

Catégories

En savoir plus sur 2-D and 3-D Plots dans Help Center et File Exchange

Produits


Version

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by