MATLAB Examples

NEXTNAME Examples

The function NEXTNAME returns a file or folder name, incrementing a number at the end of the name to ensure that the returned name is not currently used.

Contents

Basic Usage

Two inputs are required: the file or folder name (which may be an absolute or relative path) and the suffix that will get appended on the end of the name. The suffix must contain one number, which is the starting value for incrementing from. Some example suffixes are

  • '0'
  • '_1'
  • '(1)'
  • '.copy.100'
  • ' backup 00001'
  • etc...

For example, given a test directory containing three files A_1.m, A_2.m, and A_4.m (note missing '3'), then the next unused filenames will be:

name = nextname('A.m','_1') % start from one
fclose(fopen(name,'w')); % make new file with that name
name = nextname('A.m','_1')
fclose(fopen(name,'w')); % make new file with that name
name = nextname('A.m','_1')
fclose(fopen(name,'w')); % make new file with that name
name =
A_3.m
name =
A_5.m
name =
A_6.m

Start Value

The suffix must contain one number, which is used as the start value for incrementing from. The number must be an integer >=0, and NEXTNAME will find the next unused file (or folder) name, starting from that number. For example:

name = nextname('A.m','_0') % start from zero
fclose(fopen(name,'w'));
name = nextname('A.m','_0')
fclose(fopen(name,'w'));
name = nextname('A.m','_1000') % start from one thousand
fclose(fopen(name,'w'));
name = nextname('A.m','_1000')
fclose(fopen(name,'w'));
name =
A_0.m
name =
A_7.m
name =
A_1000.m
name =
A_1001.m

Folders Too!

NEXTNAME can also return folder names. For example, given the existing subfolders B(0), B(1), and B(3) in a directory (note missing '2'):

name = nextname('B','(0)')
mkdir(name); % make new folder with that name
name = nextname('B','(0)')
mkdir(name); % make new folder with that name
name = nextname('B','(0)')
mkdir(name); % make new folder with that name
name =
B(2)
name =
B(4)
name =
B(5)

Output Number Width

By default the output number has no leading zeros, which means the suffix changes length depending on the number of digits in that output number. To allow for creating fixed-width names the input sufx allows the (minimum) number width to be specified: the output number is zero padded to ensure that it has the same width as the input number has (note that leading zeros can be included in the suffix to achieve this).

Note that any leading zeros are ignored when checking for the existence of the file/folder names themselves, ensuring that all names have unique numeric values. This means that 'A_0001.m' does not replace 'A_1.m':

name = nextname('A.m','_0001')
fclose(fopen(name,'w'));
name = nextname('A.m','_0001')
fclose(fopen(name,'w'));
name =
A_0008.m
name =
A_0009.m

Sorting Names with Numbers

Once files are saved in a directory the order in which they are returned in by the OS may not be the same as the number order. The easiest way to sort filenames or directory names which contain numbers is to download my FEX submission NATSORTFILES. It is very simple to use:

S = dir('A_*.m');
sort({S.name}) % not numeric order
natsortfiles({S.name}) % numeric order
ans = 
  Columns 1 through 7
    'A_0.m'    'A_0008.m'    'A_0009.m'    'A_1.m'    'A_1000.m'    'A_1001.m'    'A_2.m'
  Columns 8 through 12
    'A_3.m'    'A_4.m'    'A_5.m'    'A_6.m'    'A_7.m'
ans = 
  Columns 1 through 8
    'A_0.m'    'A_1.m'    'A_2.m'    'A_3.m'    'A_4.m'    'A_5.m'    'A_6.m'    'A_7.m'
  Columns 9 through 12
    'A_0008.m'    'A_0009.m'    'A_1000.m'    'A_1001.m'