How to save the output of loop function with different names?

48 views (last 30 days)
I created a for loop code which gives graphs as output. How can I save the graph seperately during each iteration?

Accepted Answer

KSSV on 3 Jul 2023
for i = 1:10
fname = strcat('plot_',num2str(i),'.png') ;

More Answers (2)

Image Analyst
Image Analyst on 3 Jul 2023
help exportgraphics
EXPORTGRAPHICS Save plot or graphics content to file EXPORTGRAPHICS(OBJ, FILESPEC) saves the specified graphics to a file. OBJ is the handle of any type of an axes, a figure, a chart that can be a child of the figure, a tiled chart layout, or a container within the figure. FILESPEC is a character vector or string scalar specifying the name of a file to create. It must include the extension, and can optionally include a full or relative path. EXPORTGRAPHICS supports creating JPEG, PNG, and TIFF images, and EPS, PDF, and EMF vector formats (EMF is only supported on Windows). EXPORTGRAPHICS(___, OPTIONS) saves the graphics to the file, applying the specified OPTIONS. OPTIONS is one or more of the following name-value pairs. 'ContentType', CONTENTTYPE how to generate content in vector format files CONTENTTYPE can be one of the following values - 'vector' to generate vectorized (scalable) output - 'image' to generate rasterized output (embed image) - 'auto' allow MATLAB heuristic to decide The default value is 'auto' 'Resolution', DPI specifies output resolution for images. DPI must be a positive whole number. The default value is 150 'BackgroundColor', COLORSPEC specifies the color to use as the background COLORSPEC can be one of the following values - a color name, such as 'red', or 'w' - an RGB triplet, such as [1 0 0] or [1 1 1] - a hexadecimal color code, such as '#FFF' or '#FFFFFF') - 'none' to use either a white or transparent background, depending on what the output format supports - 'current' to use the graphic's current background color The default value is 'white' 'Colorspace', COLORSPACE the color space to use when generating output COLORSPACE can be one of the following values - 'rgb' generates RGB true color output - 'gray' generates grayscale output - 'cmyk' generates cyan, magenta, yellow, black output The default value is 'rgb' 'Append', APPEND specifies whether to append the output to an existing file or replace the content. APPEND can have a value of true or false. - true appends the output to an existing file - false replaces the content in an existing file The default value is false Example: Export a plot to a pdf file plot(rand(3)); exportgraphics(gca, 'results.pdf'); Example: Export a plot to a pdf file and ensure output is vectorized/scalable plot(rand(3)); exportgraphics(gca, 'results.pdf', 'ContentType', 'vector'); Example: Export a plot as an image to a pdf file plot(rand(3)); exportgraphics(gca, 'results.pdf', 'ContentType', 'image'); Example: Export a plot and append to an existing pdf file plot(rand(3)); exportgraphics(gca, 'results.pdf', 'Append', true); Example: Export a specific plot from the figure fig = figure; ax1 = subplot(2,1,1); bar(magic(4)); ax2 = subplot(2,1,2); plot(rand(4)); exportgraphics(ax2, 'results.png'); Example: Export all plots from figure to an image file at 300DPI subplot(2,1,1); bar(magic(4)); subplot(2,1,2); plot(rand(4)); exportgraphics(gcf, 'results.png', 'Resolution', 300); Example: Export a plot to an image file with a red background plot(rand(3)); exportgraphics(gca, 'results.png', 'BackgroundColor', [1 0 0]); Example: Export a plot to a grayscale image file plot(rand(3)); exportgraphics(gca, 'results.png', 'Colorspace', 'gray'); See also COPYGRAPHICS, EXPORTAPP Documentation for exportgraphics doc 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));
% Save the current axes to disk with the filename we just created.
exportgraphics(gca, fullFileNme);

sushma swaraj
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);
saveas(gcf, filename);
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!


Find more on Printing and Saving in Help Center and File Exchange





Community Treasure Hunt

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

Start Hunting!