필터 지우기
필터 지우기

Saving plots in For loop

조회 수: 2 (최근 30일)
Harjas
Harjas 2022년 7월 4일
댓글: Image Analyst 2022년 7월 4일
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