how can I change the code to save every figure my code does?

7 ビュー (過去 30 日間)
flashpode
flashpode 2021 年 12 月 30 日
コメント済み: Voss 2021 年 12 月 31 日
hi I've got the followed code and I want to get a diferend name for each figure my code does. How can i do it?
AllFigH = allchild(groot);
for iFig = 1:numel(AllFigH)
fig = AllFigH(iFig);
Folder = ('C:\Users\vicen\Desktop\TFG Vicenç\Imagenes\Imagenes Geoscatter\DIA 1')
FileName = ['%03d', '.png'];
saveas(fig, fullfile(Folder, FileName));
end
thank you in advance!!

採用された回答

Adam Danz
Adam Danz 2021 年 12 月 30 日
編集済み: Adam Danz 2021 年 12 月 30 日
It looks like you wanted to do,
AllFigH = allchild(groot);
for iFig = 1:numel(AllFigH)
fig = AllFigH(iFig);
Folder = ('C:\Users\vicen\Desktop\TFG Vicenç\Imagenes\Imagenes Geoscatter\DIA 1')
FileName = sprintf('%03d.png', iFig); % <-----
saveas(fig, fullfile(Folder, FileName));
end
I recommend using
AllFigH = findall(groot, 'type','figure')
rather than
AllFigH = allchild(groot);
Update: avoid overwriting filenames
This version searches the folder for png files with a naming structure of ###.png and then determines the maximum number in the file names. The new file numbers will continue with the next value.
Folder = 'C:\Users\vicen\Desktop\TFG Vicenç\Imagenes\Imagenes Geoscatter\DIA 1';
% Get all png filenames in folder.
filedata = dir(fullfile(Folder,'*.png'));
% Determine the max number in the file names ###.png
if isempty(filedata)
lastFileNumber = 0;
else
filenames = {filedata.name};
filenumStr = regexp(filenames,'^(\d+).png$','tokens','once');
filenums = str2double([filenumStr{:}]);
lastFileNumber = max(filenums);
end
% Save figures with new numbers in the file names
AllFigH = findall(groot, 'type','figure');
for iFig = 1:numel(AllFigH)
fig = AllFigH(iFig);
FileName = sprintf('%03d.png', lastFileNumber + iFig); %
saveas(fig, fullfile(Folder, FileName));
end
  31 件のコメント
flashpode
flashpode 2021 年 12 月 31 日
@Benjamin I attached my function for you to see that is not yours is it another one different created some moths ago. I wasn't using it because I thought it was not necessary and when you gave yours it gave me problems so I changed to mine
Voss
Voss 2021 年 12 月 31 日
@vicente Noguer OK, sorry for the misunderstanding. For future reference, it's a good idea to include any functions your code is dependent on so that others can run it (and to avoid any ambiguity about what they might be).

サインインしてコメントする。

その他の回答 (1 件)

Image Analyst
Image Analyst 2021 年 12 月 30 日
I would call exportgraphics() everytime you're done creating a plot. I'd create it immediately, not sometime later after all plots have been made.
  9 件のコメント
Adam Danz
Adam Danz 2021 年 12 月 30 日
@vicente Noguer, I have a feeling you're not communicating effectively and I appologize if I'm wrong about that assumption.
If you're using ui components you must be generating a uifigure. I specifially asked how you are generating the figures and you ignored my question. I also asked to share error messages and the solution in my answer should produce an error if you're using uifigures since saveas() does not work with uifigures. But you did not share that error message.
It's very difficult to help you if you're not communicating effectively.
flashpode
flashpode 2021 年 12 月 30 日
@Adam Danz I said some messages before I generate the figures with geoscatter and I upload the code(I do not know if you meant that) and I have never listened about uicomponents so thats why I was surprised. I got this warning but I could save the images the only problem I have is to save them in the folder I want. With saveas() I did not get any error of uicomponents

サインインしてコメントする。

カテゴリ

Help Center および File ExchangePrinting and Saving についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by