Import all Files of one folder in order

18 views (last 30 days)
Manuel Lauber
Manuel Lauber on 8 Nov 2020
Edited: Stephen23 on 8 Nov 2020
Hey,
i wrote a short script to import files:
path = 'C:\Users\.....';
files = dir(path);
names = {files.name};
So Matlab creates an array listing all the file names in my selected location.
My my question is if the order of the importes files can be influenced. I don´t know by which criteria MatLab loads these files into the Workspace (maybe by file size).
Is it possible to sort this array by name that way it matches my Windows order ? Cause currently the order numbering is different to these in my windows storage system (i ordered by name cause of different reasons).
Greetings
  3 Comments
Manuel Lauber
Manuel Lauber on 8 Nov 2020
Ah ok. The cell array should be ordered by name in the same way WindowsStorageSystem orders it. So the data is called:
(...)a1
(...)a2
(...)a3
and so on.
It shoulb be ordered from the lowest number to the highest.
Stephen23
Stephen23 on 8 Nov 2020
"Is it possible to sort this array by name that way it matches my Windows order ?"
Not really, for reasons that have already been discussed in detail:

Sign in to comment.

Answers (1)

Stephen23
Stephen23 on 8 Nov 2020
Edited: Stephen23 on 8 Nov 2020
If you just want an alphanumeric sort of the filenames then you can download my FEX submission:
then unzip it onto your search path (e.g. the current directory) and simply use it like this:
names = natsortfiles({files.name});

Categories

Find more on File Operations in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!