sorting list of structures in a structure

I have a list of structures in a structure.
I tried this:
%% Load data
matpath = 'C:\Users\..;
mydata = dir(fullfile(matpath ,'*.mat'));
full_file_names = fullfile(matpath,{mydata.name});
for n = 1:length(mydata)
mydata(n).data = load(full_file_names{n});
active_flow(n) = mydata(n).data;
end
The mydata(n) structure is ordered. To access of the structure I tried "active_flow(n) = mydata(n).data". But my active_flow is NOT longer ordered.
Please how can sort "active_flow" structure. See an attached screenshot of mydata structure.

1 commentaire

Stephen23
Stephen23 le 13 Nov 2023
"The mydata(n) structure is ordered... But my active_flow is NOT longer ordered."
There is nothing in the code that you show that changes the order.

Connectez-vous pour commenter.

 Réponse acceptée

Stephen23
Stephen23 le 13 Nov 2023
Modifié(e) : Stephen23 le 14 Nov 2023
It is unclear what the problem is, because you did not explain or show either the "wrong" order nor the "right" order. If you want the filenames in alpha-numeric order then you could DOWNLOAD the function NATSORTFILES:
P = 'C:\Users\..';
S = dir(fullfile(P,'*.mat'));
S = natsortfiles(S); % DOWNLOAD if you want alpha-numeric order of filenames
for n = 1:numel(S)
F = fullfile(S(n).folder,S(n).name);
S(n).data = load(F);
end
active_flow = [S.data] % optional, if all MAT files contain the same variable names

4 commentaires

University
University le 13 Nov 2023
Hi Stehen,
Thank you for your response.
I download the 'natsortfiles' function and to my file path but I still receive this error:
'natsortfiles' is not found in the current folder or on the MATLAB path, but exists in:
Change the MATLAB current folder or add its folder to the MATLAB path.
which folder the function suppose to be in?
Dyuman Joshi
Dyuman Joshi le 13 Nov 2023
Enter the command 'cd' in the command window and see the output.
Stephen23
Stephen23 le 13 Nov 2023
"which folder the function suppose to be in?"
It is exactly as with your own files: either in the current directory (simpler) or on the MATLAB Search Path:
University
University le 13 Nov 2023
Thank you Stephen. It has worked.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

Produits

Version

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by