load multiple files in right order

Hello,
I'm loading multiple mat-files in workspace, the names of files are:
Schweben_Meg_2_2-01_SNR_-030_PLL.mat
Schweben_Meg_2_2-01_SNR_-015_PLL.mat
Schweben_Meg_2_2-01_SNR_000_PLL.mat
Schweben_Meg_2_2-01_SNR_015_PLL.mat
Schweben_Meg_2_2-01_SNR_030_PLL.mat
the answers will be writen to one result files. The problem - MATLAB loads this files in "wrong" order.
First it takes:
Schweben_Meg_2_2-01_SNR_-015_PLL.mat
than
Schweben_Meg_2_2-01_SNR_-030_PLL.mat
than
Schweben_Meg_2_2-01_SNR_000_PLL.mat
.....
I want to get a loading order:
Schweben_Meg_2_2-01_SNR_-030_PLL.mat first
than
Schweben_Meg_2_2-01_SNR_-015_PLL.mat
than
Schweben_Meg_2_2-01_SNR_000_PLL.mat
it is possible to read a data in "right" order without rename of all the files? (I have about 1000x of thoose files)
Thank you!

2 Comments

I guess you have to isolate the PLL(?) values and sort them separately.
Then re-build the filenames to be loaded.
Thanks, I try to implement a natsortfiles-function now.
Actually, I made already a workaround tool to resort my results. Small script that turn a first and second row of result. But it's not really nice =)

Sign in to comment.

 Accepted Answer

Stephen23
Stephen23 on 1 Dec 2020
Edited: Stephen23 on 18 Apr 2021
You could download my FEX submission natsortfiles:
and use the regular expression '-?\d{3}' (or similar), e.g.:
D = 'path to the folder where the files are saved';
S = dir(fullfile(D,'Schweben*PLL.mat'));
S = natsortfiles(S,'-?\d{3}'); % alphanumeric sort by filename
... etc

9 Comments

Hello! Than you, I tried this and get:
sys_var =
5×1 struct array with fields:
name
folder
date
bytes
isdir
datenum
__________________________________________________________
sys_var.name
ans =
'Schweben_Meg_2_2-01_SNR_-015_PLL.mat'
'Schweben_Meg_2_2-01_SNR_-030_PLL.mat'
'Schweben_Meg_2_2-01_SNR_000_PLL.mat'
'Schweben_Meg_2_2-01_SNR_015_PLL.mat'
'Schweben_Meg_2_2-01_SNR_030_PLL.mat'
_____________________________________________________________
%Implementation with your code
____________________________________________________________
sys_var(1).folder = '/home/nik/workspace/QT/Software_2.0_QT/IO/Schweben_Meg_2_2-01/Schweben_Meg_2_2-01_SNR_F0_F1' % equals your D
_____________________________________________________________
S = dir(fullfile(sys_var(1).folder,'Schweben*PLL.mat')); %second step
S =
5×1 struct array with fields:
name
folder
date
bytes
isdir
datenum
__________________________________
S.name
ans =
'Schweben_Meg_2_2-01_SNR_-015_PLL.mat'
'Schweben_Meg_2_2-01_SNR_-030_PLL.mat'
'Schweben_Meg_2_2-01_SNR_000_PLL.mat'
'Schweben_Meg_2_2-01_SNR_015_PLL.mat'
'Schweben_Meg_2_2-01_SNR_030_PLL.mat'
______________________________________
C = natsortfiles({S.name},'-?\d{3}')
C =
1×5 cell array
Columns 1 through 3
{'Schweben_Meg_2_…'} {'Schweben_Meg_2_…'} {'Schweben_Meg_2_…'}
Columns 4 through 5
{'Schweben_Meg_2_…'} {'Schweben_Meg_2_…'}
_________________________________________
C(1)
ans =
1×1 cell array
{'Schweben_Meg_2_2-01_SNR_-030_PLL.mat'}
....
___________________________________________
Order is right now, but how can I put it back to sys_var variable? Its not more struct, its a cell array!
Thank you!
Use the second output of natsortfiles to sort the original struct.
It is right there under the "Output 2: Sort Index" section.
It sort in wrong order: 000, 015, 030, -015, -030
and I need: -030, -015,000,015,030
_______________________________________
%N = natsortfiles({sys_var.name})
D = sys_var(1).folder
S = dir(fullfile(D,'Schweben*PLL.mat'))
[~,ndx] = natsortfiles({S.name})
ndx =
3 4 5 1 2
As Rik wrote, you can use the second output to sort the structure array returned by dir:
S = dir(fullfile(D,'Schweben*PLL.mat'))
[~,ndx] = natsortfiles({S.name},'-?\d{3}');
S = S(ndx)
This is shown in the Examples documentation, in the section entitled "Example with DIR and a Structure".
Note that you also need to use the second input to natsortfiles (exactly as shown in my answer) as this is what tells natsortfiles to take the negative sign into consideration.
Curiously the link that you gave here is for a very old version of the Examples documentation, the most up-to-date version is always included with the FEX submission itself (you can view it by clicking on the Examples tab):
I have no idea why TMW has outdated versions of the documentation still available on its website (and even worse: apparently highly ranked by some prominent internet search engines), but for any FEX submission you should always refer to the help included with the submission itself.
Thank you Stephen, it works great!
sys_var = dir('**/*.mat'); % looking for directories with mat-files and save to struct
D = sys_var(1).folder; % buffering a path and folder name of files
S = dir(fullfile(D,'*PLL.mat')); % make struct with path, folder name and filename
[~,ndx] = natsortfiles({S.name},'-?\d{3}'); % sorting filenames and saving new index
S = S(ndx); % sorting struct S with new index
sys_var = S; % reinitialize a sys_var variable with new struct
I think is over now!
sys_var = dir('**/*.mat');
[~,ndx] = natsortfiles({sys_var.name},'-?\d{3}');
sys_var = sys_var(ndx);
"Thank you Stephen, it works great! "
Please remember to accept my answer if it helped you.
Great, short and nice!

Sign in to comment.

More Answers (0)

Categories

Asked:

on 1 Dec 2020

Edited:

on 18 Apr 2021

Community Treasure Hunt

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

Start Hunting!