Call matfiles in a loop and save the graph without overwriting the former ones

1 visualización (últimos 30 días)
Hello Community,
I'm trying to write loop for data processing. The script should load a matfile and create a graph for every file with subsequent saving the files.
The data processing works just fine and I see that the figures are created. I process 36 matfiles and after the run, 36 figures are created in 36 matlab windows, but in the folder are the 36 matfiles and only 1 svg. The files are saved but the next overwrites the former. The indexing is not working.
I used that script:
clc,close all
matfiles = dir('*.mat') ;
N = length(matfiles) ;
for i = 1:N
load(matfiles(i).name)
...
% etc. script loads data
figure('Units','centimeters','Position',[0 0 25 16],'PaperPositionMode','auto');
% creates a plot
...
set(gcf,'PaperPositionMode','auto')
filename = ['plot',num2str(i),'.svg']
saveas(gcf,filename,'svg');
end
Best regards
S. B.

Respuesta aceptada

Sulaymon Eshkabilov
Sulaymon Eshkabilov el 3 de Sept. de 2021
You should start your code with clearvars, e.g:
clearvars
delete plot*.svg
%%
Then try this syntax:
% then try this syntax:
...
for ii = 1:N
A = load(matfiles(i).name);
% Make sure that the loaded data is correctly chosen to plot it
plot(A.data) % Note this
...
Filename = strcat(['plot',num2str(ii),'.svg'])
%saveas(gcf,Filename,'svg'); % Works perfectly OK that I have tested, but you should try this one instead.
saveas(gcf,Filename);
end
Note that it is a good programming habit to use "ii" instead of "i".

Más respuestas (1)

Sulaymon Eshkabilov
Sulaymon Eshkabilov el 3 de Sept. de 2021
% Try this
...
filename = strcat(['plot',num2str(i),'.svg'])
saveas(gcf,filename,'svg');
  3 comentarios
Steffen Bxyz
Steffen Bxyz el 7 de Sept. de 2021
Thanks for your answer,
it solves the problem.
Now it's working like intended
Best regards
S.B.

Iniciar sesión para comentar.

Categorías

Más información sobre Function Creation en Help Center y File Exchange.

Productos


Versión

R2015b

Community Treasure Hunt

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

Start Hunting!

Translated by