Solve Eror Saveas nome

1 次查看(过去 30 天)
MartinM
MartinM 2023-2-20
评论: Rik 2023-2-21
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
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
Rik 2023-2-20
Perhaps you meant saveas(gcf,[num2str(zz) '.png']) instead, since you never define kk in your code.
  3 个评论
MartinM
MartinM 2023-2-20
I have try
saveas(gcf,[sprintf('%1d',kk),'.png'])
saveas(gcf,[convertStringsToChars(name(zz)) '\' strcat(num2str(kk),'.png')])
Rik
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
this call to saveas will write the file Pedestal_AC/1.png this call to saveas will write the file Pc_Ecart/1.png this call to saveas will write the file ratioE2FWHM/1.png
this call to saveas will write the file ratioDT_AC/1.png
this call to saveas will write the file lim75E_AC/1.png
this call to saveas will write the file RatioI/1.png
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 CenterFile Exchange 中查找有关 Printing and Saving 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by