Solve Eror Saveas nome

2 Ansichten (letzte 30 Tage)
MartinM
MartinM am 20 Feb. 2023
Kommentiert: Rik am 21 Feb. 2023
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 Kommentar
Walter Roberson
Walter Roberson am 20 Feb. 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

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Rik
Rik am 20 Feb. 2023
Perhaps you meant saveas(gcf,[num2str(zz) '.png']) instead, since you never define kk in your code.
  3 Kommentare
MartinM
MartinM am 20 Feb. 2023
I have try
saveas(gcf,[sprintf('%1d',kk),'.png'])
saveas(gcf,[convertStringsToChars(name(zz)) '\' strcat(num2str(kk),'.png')])
Rik
Rik am 21 Feb. 2023
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

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Printing and Saving finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by