Can anyone help me?

2 views (last 30 days)
Mini
Mini on 2 Jan 2015
Commented: Geoff Hayes on 5 Jan 2015
Each run of my program plots a figure. Can anyone help me to run the program 50 times and save each figure into a file?

Accepted Answer

Geoff Hayes
Geoff Hayes on 2 Jan 2015
Edited: Geoff Hayes on 2 Jan 2015
Mini - do you want to save the figure to a fig file or to some other data type? If the former, then you would use savefig, and if the latter you would use saveas. As for running your program fifty times, you could do something like this
numIters = 50;
for k=1:numIters
% close the current figure
close gcf;
% call your program that plots a figure
% save the current figure to file
filename = sprintf('myFigure%d.fig',k);
savefig(gcf,filename);
end
Note how we create a unique filename on each iteration of the for loop. You can modify the above to use the saveas function instead, or to specify a different file name and/or folder for that file.
Note how we use gcf which gets the current figure handle which will be the one that your program just created.
  2 Comments
Mini
Mini on 5 Jan 2015
Geoff Hayes - Thank you for the answer. My Matlab is version R2010a and in that savefig is not working. But i got it working with saveas as a fig file itself. Mini.
Geoff Hayes
Geoff Hayes on 5 Jan 2015
Glad that you got it working, Mini!

Sign in to comment.

More Answers (0)

Categories

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!