Saving plots in For loop
1 次查看(过去 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'])
0 个评论
采纳的回答
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()".
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
更多回答(2 个)
Image Analyst
2022-7-4
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 个评论
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 Center 和 File Exchange 中查找有关 Printing and Saving 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!