How to save the output of loop function with different names?
48 views (last 30 days)
More Answers (2)
Image Analyst on 3 Jul 2023
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.
% Save the current axes to disk with the filename we just created.
sushma swaraj on 6 Jul 2023
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);
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!