automatically index .fig files

2 views (last 30 days)
Wouter
Wouter on 23 Apr 2014
Answered: Jan on 23 Apr 2014
I have a function that generates graphs which become saved in a .fig file. Now, I want to run this function multiple times with different parameters. Is it possible for matlab to save all these fig files(let's say file.fig, file(1).fig, file(2).fig) or will it overwrite the old one file.fig each time? I prefer not to put all the input arguments of my function in the name.
The function is called omegac4.m and has four input arguments(a cell array IN which defines initial conditions, wmin and wmax are doubles and a natural number N). I automatically put a scalar(IN{1}) in the name of the .fig file via sprintf. The program divides the [wmin,wmax] intervals in N steps w(n) and for each w I call a function vortex9(IN,w) I times(I define I in the beginning of my script, it's 10000 right now but it's possible I'll change it later on). For each w(n) I take the average of the I results of vortex9 and save them in v(n). At the end of omegac4 I write w(n) and v(n) in a txt-file(the new text is added to the old one at the bottom) and I make a plot of v as a function of w. I want to save this plot as a figure, and if I run omegac4 a second time (with the same IN{1}) I want to store the new plot, but don't lose the old one.
It's some kind of simulation dependent on random numbers, a run takes a couple of hours and afterwards I decide each time with which new parameters I want to run it (possibly the same)

Accepted Answer

Jan
Jan on 23 Apr 2014
I assume, that you want to determine a new file name automatically. If you do not want to rely on tempname, which creates unique, but strange looking names, you require a dedicated function like this:
function Name = FindNewName(Folder, Pattern, Spec)
% Input:
% Folder: Folder name
% Pattern: File name with a star for the counter
% Spec: Number format for SPRINTF
% Example:
% FindNewName(cd, 'Figure*.fig', '%.3d')
List = dir(fullfile(Folder, Pattern));
Num = length(List) + 1;
Fmt = strrep(Pattern, '*', Spec);
Name = sprintf(Fmt, Num);
% Care for deleted files:
while exist(fullfile(Folder, Name), 'file')
Num = Num + 1;
Name = sprintf(Fmt, Num);
end

More Answers (1)

Azzi Abdelmalek
Azzi Abdelmalek on 23 Apr 2014
for k=1:10
filename=sprintf('file%d.fig',k)
%do your plot and save your figure
end
  3 Comments

Sign in to comment.

Categories

Find more on Graphics Performance in Help Center and File Exchange

Tags

Products

Community Treasure Hunt

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

Start Hunting!