Solve Eror Saveas nome
조회 수: 1 (최근 30일)
이전 댓글 표시
Hello
I try to save figure gcf, in different folder.
Each loop a figure, in a new folder.
name = [ "Pedestal_AC"
"Pc_Ecart"
"ratioE2FWHM"
"ratioDT_AC"
"lim75E_AC"
"RatioI"]
kk=1;
for zz=1:6%length(name)
folder = convertStringsToChars(name(zz));
if ~isdir(folder)
mkdir(folder);
end
cd (folder)
frame = getframe(zz);
im = frame2im(frame);
[imind,cm] = rgb2ind(im,256);
figure(zz)
% saveas(gcf,[convertStringsToChars(name(zz)) '\' strcat(num2str(kk),'.png')])
saveas(gcf,[ num2str(kk) '.png'])
end
If I run [ num2str(kk) '.png'] , it provides me : '1.png'
but if I run the code :
Error using matlab.graphics.internal.name (line 112)
Cannot create output file '.\1.png'.
Can not understand
If you have solution I will take it
regards
댓글 수: 1
Walter Roberson
2023년 2월 20일
note that you never cd back.
You should avoid using cd. Use fullfile(folder, kk+".png")
Also you are assuming that if the folder already exists that you have permission to write into it, which might not be true
답변 (1개)
Rik
2023년 2월 20일
Perhaps you meant saveas(gcf,[num2str(zz) '.png']) instead, since you never define kk in your code.
댓글 수: 3
Rik
2023년 2월 21일
Neither of those should result in the error you posted. Perhaps you want something like the code below.
Do you see how the indentation already cleans up your post? Now you only need to write comments explaining why you're using this code.
name = [ "Pedestal_AC"
"Pc_Ecart"
"ratioE2FWHM"
"ratioDT_AC"
"lim75E_AC"
"RatioI"];
if numel(unique(name))~=numel(name) % check for duplicates
error('this will result in overwritten files')
end
kk=1;
for zz=1:numel(name)
folder = convertStringsToChars(name(zz));
if ~isdir(folder)
mkdir(folder);
end
filename = fullfile(folder,strcat(num2str(kk),'.png'));
f = figure(zz);
if zz==numel(name) % since these all get overwritten, we only need to do the last
frame = getframe(f);
im = frame2im(frame);
[imind,cm] = rgb2ind(im,256);
end
saveas(f,filename)
end
function saveas(f,fn)
% Overload saveas to show what would happen
if exist(fileparts(fn),'dir')
fprintf('this call to saveas will write the file %s\n',fn);
else
fprintf('an error will occur')
end
% close the figure to avoid cluttering the post with empty images
close(f);
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!