Loading multiple structure from a folder

I am tried load multiple structures from a folder but I got a error
below:
filepath = '..\sol_active_case11\';
mydata = dir(fullfile(filepath,'*.mat'));
full_file_names = fullfile(filepath,{mydata.name});
%% natsortfiles: Sorts text by character code and by number value
% By default matches integer substrings and performs a case-insensitive ascending sort.
% Options to select the number format, sort order, case sensitivity, etc.
mydata = natsortfiles(mydata);
%
for n = 1:numel(mydata)
F = fullfile(mydata(n).folder,mydata(n).name);
mydata(n).data = load(F);
end
A = [mydata.data];
Error using horzcat Number of fields in structure arrays being concatenated do not match.
Concatenation of structure arrays requires that these arrays have the same set of fields.
Error in Active_flow1D_case11_plot (line 23)
A = [mydata.data];

 Accepted Answer

Matt J
Matt J on 24 Feb 2024
Edited: Matt J on 24 Feb 2024
It is because not all of the sub-structures mydata(n).data have the same fields. If you contest that, attach the mydata variable in a .mat file for us so that we can all examine it.

7 Comments

Not the data I asked requrested. I asked for a single .mat file containing the mydata variable generated by the code.
As you can see below, your mydata(i).data are not all of the same fieldd structure. In particular, mydata(4) contains an additional field N.
load Single
mydata(1).data
ans = struct with fields:
nt: 11 pars: [1×1 struct] tarray: [0 1 2 3 4 5 6 7 8 9 10] theta: [11×201 double] v: [11×201 double] xival: -0.0600 z: [0 1.0000e-06 2.0000e-06 3.0000e-06 4.0000e-06 5.0000e-06 6.0000e-06 7.0000e-06 8.0000e-06 9.0000e-06 1.0000e-05 1.1000e-05 1.2000e-05 1.3000e-05 1.4000e-05 1.5000e-05 … ] (1×201 double)
mydata(4).data
ans = struct with fields:
N: 200 nt: 11 pars: [1×1 struct] tarray: [0 1 2 3 4 5 6 7 8 9 10] theta: [11×201 double] v: [11×201 double] xival: 0.6000 z: [0 1.0000e-06 2.0000e-06 3.0000e-06 4.0000e-06 5.0000e-06 6.0000e-06 7.0000e-06 8.0000e-06 9.0000e-06 1.0000e-05 1.1000e-05 1.2000e-05 1.3000e-05 1.4000e-05 1.5000e-05 … ] (1×201 double)
One remedy would be to use a cell aray instead,
A={mydata.data};
Oh, thank you so much
You're welcome, but please Accept-click the answer if the question is resolved.

Sign in to comment.

More Answers (0)

Categories

Products

Release

R2023b

Asked:

on 23 Feb 2024

Commented:

on 24 Feb 2024

Community Treasure Hunt

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

Start Hunting!