Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Sequential counting with placeholder zeros

Subject: Sequential counting with placeholder zeros

From: Nicolas

Date: 9 Apr, 2010 06:59:06

Message: 1 of 2

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.

Subject: Sequential counting with placeholder zeros

From: Walter Roberson

Date: 9 Apr, 2010 14:40:26

Message: 2 of 2

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.

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us