Solve Eror Saveas nome

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 comentario

Walter Roberson
Walter Roberson el 20 de Feb. de 2023
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

Iniciar sesión para comentar.

Respuestas (1)

Rik
Rik el 20 de Feb. de 2023

1 voto

Perhaps you meant saveas(gcf,[num2str(zz) '.png']) instead, since you never define kk in your code.

3 comentarios

MartinM
MartinM el 20 de Feb. de 2023
Dear Rik
Thank for your answer,
kk is define on the top of the code, I only paste a part.
Sorry for that.
I add it
MartinM
MartinM el 20 de Feb. de 2023
I have try
saveas(gcf,[sprintf('%1d',kk),'.png'])
saveas(gcf,[convertStringsToChars(name(zz)) '\' strcat(num2str(kk),'.png')])
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

Iniciar sesión para comentar.

Categorías

Más información sobre MATLAB en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

el 20 de Feb. de 2023

Comentada:

Rik
el 21 de Feb. de 2023

Community Treasure Hunt

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

Start Hunting!

Translated by