From: Nicolas on
This may be more of an algorithm question rather than a coding question--

I'm trying to access file names in a sequential manner, and usually it wouldn't be an issue, except that the file names are assigned as follows: file0000, file0001, file0002, etc.

Those placeholder zero's really mess things up! If I use something like 'file*', should I worry about Matlab mixing up the sequence? (Especially when I'm going through 200+ files)

What would be a good way to approach this in a way that I could be certain that every file was accessed sequentially?

Any input is appreciated.
From: Walter Roberson on
Nicolas wrote:
> This may be more of an algorithm question rather than a coding question--
>
> I'm trying to access file names in a sequential manner, and usually it
> wouldn't be an issue, except that the file names are assigned as
> follows: file0000, file0001, file0002, etc.
>
> Those placeholder zero's really mess things up! If I use something like
> 'file*', should I worry about Matlab mixing up the sequence? (Especially
> when I'm going through 200+ files)
>
> What would be a good way to approach this in a way that I could be
> certain that every file was accessed sequentially?

The order that the dir() function returns the files is not defined by
Matlab, so if you need to process files in a particular sequence, you
should apply an appropriate sorting algorithm to the 'name' field of the
structure that dir() returns.

For example, one of several methods:

finfo = dir('file*');
[vals, orderidx] = sortrows(char({finfo.name}));
finfo = finfo(orderidx);

Then finfo(1).name would be the first file name, finfo(2).name would be
the second file name, and so on.


By the way, the "placeholder zeros" make the sorting considerably
_easier_ than if they were not there, as otherwise file19 would sort
before file2 . If you want to do a numeric sort, it is better to have
those leading zeros in place.