IRootLab
An Open-Source MATLAB toolbox for vibrational biospectroscopy
find_filename.m
Go to the documentation of this file.
1 %>@ingroup ioio
2 %>@file
3 %>@brief Finds a name for a new file.
4 %>
5 %> File name is composed as
6 %>@code
7 %>name = [prefix sprintf('_%04d', i) suffix '.' extension];
8 %>@endcode
9 %>
10 %> @param prefix
11 %> @param suffix
12 %> @param extension
13 %> @param flag_return_ext =1. If 0, will return the file name only, leaving to the called the job to complete with the extension
14 function name = find_filename(prefix, suffix, extension, flag_return_ext)
15 
16 if ~exist('suffix', 'var')
17  suffix = '';
18 end;
19 
20 if ~exist('extension', 'var')
21  extension = 'txt';
22 end;
23 
24 if nargin < 4 || isempty(flag_return_ext)
25  flag_return_ext = 1;
26 end;
27 
28 
29 files = dir(['*.' extension]);
30 flag_ok = 0;
31 for i = 1:9999
32  name = [prefix sprintf('_%04d', i) suffix '.' extension];
33 
34  flag_exists = 0;
35  for j = 1:length(files)
36  if strcmp(name, files(j).name)
37  flag_exists = 1;
38  break;
39  end;
40  end;
41 
42  if ~flag_exists
43  flag_ok = 1;
44  break;
45  end;
46 end;
47 
48 if ~flag_ok
49  irerror('Could not find a file name, gave up!');
50 end;
51 
52 
53 if ~flag_return_ext
54  [who, name, cares] = fileparts(name); %#ok<NASGU,ASGLU>
55 end;
function irerror(in s)
Analysis Session (AS) base class.
Definition: as.m:6
function find_filename(in prefix, in suffix, in extension, in flag_return_ext)