using fopen, issue with the sequence files are opened

2 vues (au cours des 30 derniers jours)
Nicolas
Nicolas le 26 Août 2013
I am having a problem with fopen. I am opening a sequence of snapshots, however 1001 is being treated as the next in the sequence to 100. So fopen is opening files in the following order:
snapshot_100
snapshot_1001
snapshot_1002
snapshot_1003
...
snapshot_101
snapshot_1010
snapshot_1011
Is there a way to get these to open in the correct sequence? My code is as follows:
files=dir(fullfile(dirname, 'snapshot*'));
for i=1:numfiles
files(i).name=fopen(files(i).name,'r','n');
fseek(files(i).name,264,'bof');
[~,count]= fread(files(i).name,1,'int32');
posdata{i}=fread(files(i).name,[3,N],'*float32','n');
[~,count]= fread(files(i).name,1,'int32');
posdata{i}=posdata{i}';
x{i}=posdata{i}(1:N,1);
y{i}=posdata{i}(1:N,2);
z{i}=posdata{i}(1:N,3);
...
I cannot change the number of digits on the snapshots. Any help would be welcome.
  1 commentaire
Walter Roberson
Walter Roberson le 26 Août 2013
Note: dir() returns the filenames in the order returned by the operating system. In MS Windows, the operating system returns the files in the order stored on the file system. Some of the file systems use byte-level sorting, but some of the less common MS Windows file systems use other orders (or no particular order.)
In short: you should not rely on the order of names returned by dir()

Connectez-vous pour commenter.

Réponse acceptée

Azzi Abdelmalek
Azzi Abdelmalek le 26 Août 2013
You can sort your files like this:
files={'snapshot_100'
'snapshot_1001'
'snapshot_1002'
'snapshot_1003'
'snapshot_101'
'snapshot_1010'
'snapshot_1011'}
[ii,jj]=sort(cellfun(@str2double,regexp(files,'\d+','match')))
files=files(jj)
  1 commentaire
Azzi Abdelmalek
Azzi Abdelmalek le 26 Août 2013
Modifié(e) : Azzi Abdelmalek le 26 Août 2013
[Nicolas commented]
Thanks for the help.
Nicolas, to add a comment click on [comment on this answer]

Connectez-vous pour commenter.

Plus de réponses (2)

Image Analyst
Image Analyst le 26 Août 2013
Parse the filenames so that you extract the numbers into an integer array, then sort the array and get the indexes
[sortedNumbers, sortIndexes] = sort(numberArray);
% Then apply the same sorting order onto your filename array.
files = files(sortIndexes);
(Note: off the top of my head - not tested.)

Walter Roberson
Walter Roberson le 26 Août 2013
There is a FEX contribution to sort filenames that contain embedded numbers.
  3 commentaires
Azzi Abdelmalek
Azzi Abdelmalek le 26 Août 2013

Connectez-vous pour commenter.

Catégories

En savoir plus sur Get Started with MATLAB dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by