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

7 visualizaciones (últimos 30 días)
flashpode
flashpode el 30 de Dic. de 2021
Comentada: Voss el 31 de Dic. de 2021
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!!

Respuesta aceptada

Adam Danz
Adam Danz el 30 de Dic. de 2021
Editada: Adam Danz el 30 de Dic. de 2021
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 comentarios
flashpode
flashpode el 31 de Dic. de 2021
@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 el 31 de Dic. de 2021
@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).

Iniciar sesión para comentar.

Más respuestas (1)

Image Analyst
Image Analyst el 30 de Dic. de 2021
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 comentarios
Adam Danz
Adam Danz el 30 de Dic. de 2021
@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 el 30 de Dic. de 2021
@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

Iniciar sesión para comentar.

Categorías

Más información sobre Printing and Saving en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by