Sorting an array of strings based on number pattern
I’m looking for an _efficient_ way to solve the following issue:
Typically I read out all files in a directory that match a certain pattern by means of the *dir* function:
dirFiles = dir(‘Common_*_common.ext’);
fileNames= {dirFiles.name};
This does what it is supposed to do and gives me a nice cell array with files I need to load.
However, the pattern is of the format _0, 1, 2, …, 8, 9, 10_ and the order of my *fileNames* comes out as _0, 10, 1, …, 7, 8, 9_. Unfortunately, changing the filenames themselves to _00, 01, 02, …, 10_ is not an option. Is there an efficient way to sort such arrays to the correct format?
Of course this should also hold when the sequence increases to larger numbers (e.g. _0, 1, 2, …, 29, 30, 31_).I’m looking for an _efficient_ way to solve the following issue:
Typically I read out all files in a directory that match a certain pattern by means of the *dir* function:
dirFiles = dir(‘Common_*_common.ext’);
fileNames= {dirFiles.name};
This does what it is supposed to do and gives me a nice cell array with files I need to load.
However, the pattern is of the format _0, 1, 2, …, 8, 9, 10_ and the order of my *fileNames* comes out as _0, 10, 1, …, 7, 8, 9_. Unfortunately, changing the filenames themselves to _00, 01, 02, …, 10_ is not an option. Is there an efficient way to sort such arrays to the correct format?
Of course this should also hold when the sequence increases to larger numbers (e.g. _0, 1, 2, …, 29, 30, 31_). I’m looking for an _efficient_ way to solve the following issue:
Typically I read out all files in a directory that match a certain pattern by means of the *dir* function:
dirFiles = dir(‘Common_*_common.ext’);
fileNames= {dirFiles.name};
This does what it is supposed to do and gives me a nice cell array with files I need to load.
However, the pattern is of the format _0, 1, 2, …, 8, 9, 10_ and the order of my *fileNames* comes out as _0, 10, 1, …, 7, 8, 9_. Unfortunately, changing the filenames themselves to _00, 01, 02, …, 10_ is not an option. Is there an efficient way to sort such arrays to the correct format?
Of course this should also hold when the sequence increases to larger numbers (e.g. _0, 1, 2, …, 29, 30, 31_). sort, strings, pattern, sequence, natsort, natsortfiles MATLAB Answers — New Questions