How to save the output of loop function with different names?
58 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Faris
el 3 de Jul. de 2023
Respondida: sushma swaraj
el 6 de Jul. de 2023
I created a for loop code which gives graphs as output. How can I save the graph seperately during each iteration?
2 comentarios
Respuesta aceptada
KSSV
el 3 de Jul. de 2023
for i = 1:10
fname = strcat('plot_',num2str(i),'.png') ;
plot(rand(1,10))
saveas(gcf,fname)
end
0 comentarios
Más respuestas (2)
Image Analyst
el 3 de Jul. de 2023
help exportgraphics
Sample code snippet. Adapt as needed:
folder = pwd; % or 'C;\wherever you want'
for k = 1 : 5
% Create output filename.
baseFileName = sprintf('Plot %2.2d.png', k);
fullFilename = fullfile(folder, baseFileName);
% Make up your graphs however you want.
plot(rand(1, 10));
xlabel('x');
ylabel('y');
% Save the current axes to disk with the filename we just created.
exportgraphics(gca, fullFileNme);
end
0 comentarios
sushma swaraj
el 6 de Jul. de 2023
Hi,
To save each graph separately during each iteration of a for loop in MATLAB, you can use the saveas function. Here's an example of how you can modify your code to save the graphs:
for i = 1:N
% Add your code to generate the graph
filename = sprintf('graph_%d.png', i);
saveas(gcf, filename);
end
In this example, the for loop iterates N times. Inside the loop, you generate the graph using your code.After generating the graph, you can save it using the saveas function. The gcf command retrieves the handle of the current figure.
Hope it works!
0 comentarios
Ver también
Categorías
Más información sobre Printing and Saving en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!