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 件のコメント

Timo Dietz
Timo Dietz 2020 年 12 月 1 日
I guess you have to isolate the PLL(?) values and sort them separately.
Then re-build the filenames to be loaded.
Nik Rocky
Nik Rocky 2020 年 12 月 1 日
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 =)

サインインしてコメントする。

 採用された回答

Stephen23
Stephen23 2020 年 12 月 1 日
編集済み: Stephen23 2021 年 4 月 18 日

0 投票

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 件のコメント

Nik Rocky
Nik Rocky 2020 年 12 月 1 日
編集済み: Nik Rocky 2020 年 12 月 1 日
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!
Rik
Rik 2020 年 12 月 1 日
Use the second output of natsortfiles to sort the original struct.
Nik Rocky
Nik Rocky 2020 年 12 月 1 日
Rik
Rik 2020 年 12 月 1 日
It is right there under the "Output 2: Sort Index" section.
Nik Rocky
Nik Rocky 2020 年 12 月 1 日
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
Stephen23
Stephen23 2020 年 12 月 2 日
編集済み: Stephen23 2020 年 12 月 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.
Nik Rocky
Nik Rocky 2020 年 12 月 2 日
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!
Stephen23
Stephen23 2020 年 12 月 2 日
編集済み: Stephen23 2020 年 12 月 2 日
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.
Nik Rocky
Nik Rocky 2020 年 12 月 2 日
Great, short and nice!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeFile Operations についてさらに検索

質問済み:

2020 年 12 月 1 日

編集済み:

2021 年 4 月 18 日

Community Treasure Hunt

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

Start Hunting!

Translated by