Storing the plots in a folder while running the MATLAB program?

1

In my MATLAB program I have to run a for loop for 500 times and each time the loop runs it plots a graph, so If I run the program there will be 500 (.fig files) and that may hang my system.

So is there any way that I can save the output which are produced after each loop automatically in some folder?.

If there is some procedure, a reference to that procedure will be very much helpful!.

BAYMAX

Posted 2018-01-10T07:43:55.743

Reputation: 125

Answers

1

You can use the saveas method.

For example, to save a simple bar plot as a png file:

x = [2 4 7 2 4 5 2 5 1 4];
bar(x);
saveas(gcf,'Barchart.png')

or as an eps file:

saveas(gcf,'Barchart','epsc')

Make sure you use a filename that depends on something that varies in each loop iteration to not overwrite the file. You can use sprintf to create the new file name, e.g. to save an eps file:

for k = 1:500
    filename = sprintf('%s_%d','Barchart',k);
    % Create the plot
    saveas(gcf,filename,'epsc')
end

See the link to the documentation for more configurations and filetypes.

Shaido - Reinstate Monica

Posted 2018-01-10T07:43:55.743

Reputation: 1 195

how to change the name of the file in each iteration, like barchart1,barchart2,barchart3,... ?? – BAYMAX – 2018-01-17T16:25:40.430

@BAYMAX: You can, for example, use sprintf to do this. I added an example in the answer. – Shaido - Reinstate Monica – 2018-01-18T01:14:45.360