Array from structs in a workspace

Good day.
I’m a little bit mixt up with chars, doubles, operands, and so on…
1st. I’m loading a bunch of structs from directory to workspace.
files=dir('C:\...\*.mat');
for i=1:length(files)
load(files(i).name);
end
I want to make from them an array.
array=[name1 name2 ... name118]
This method produces expected outcome – a struct 1x118. The only undesirable thing is to type all the 118 names “manually”.
I trade some “shortcuts”…
array = files;
Produces incorrect array 118x1 with some data about files, not their fields.
array =(files(i).name);
Asks to check for missing argument or incorrect argument data type in call to further function 'struct2cell'.
array =(1:files(i).name);
Error due to colon operator with char operands, first and last operands must be char.
Search in manuals and “ask” section is still in progress. Who – also can't fit, because at that point of the script are additional/not .mat variables (files and i) in workspace.
Maby someone has a hint on how I could avoid typing in “manual”?

2 Comments

Do each of the files have exactly the same variable names in exactly the same order?
Files (.mat) have all the different names (Ac Ag Al…), structure inside them is similar, with the same names for fields.

Sign in to comment.

 Accepted Answer

Walter Roberson
Walter Roberson on 16 Aug 2020
Edited: Walter Roberson on 16 Aug 2020
projectdir = 'C:\...';
dinfo = dir( fullfile(projectdir, '*.mat') );
filenames = fullfile( {dinfo.folder}, {dinfo.name} );
for i = length(files) : -1 : 1
array(1,i) = load(filenames{i});
end
The backwards loop is for efficiency, as it initializes the struct to full size and so does not need to keep extending the struct like you would if you looped forwards.

4 Comments

Thank You for sharing your script. Unfortunately, I couldn't manage the implementation of the reverse loop. I decided to stick to an ordinary one.
projectdir = 'C:\...\elements';
dinfo = dir( fullfile(projectdir, '*.mat') );
filenames = fullfile( {dinfo.name} );
for i = 1:length(filenames)
[filenames{i}];
end
array=[filenames];
cells=struct2cell(array); % converts struct to cell matrix
sortvals=cells(1,1,:); % converts the values of just first field
In line cells=struct2cell(array) input is array -1X118 cell array ({‘Ac.mat’} {‘Ag.mat’} … and so on). My main goal vas to get a 1X118 struct array with fields. That array’s elements/structs would have their internal fields with values. And then sort them according to one of the fields "(1,1,:)", from smalest to bigest value.
Unfortunately, for my, substitution of listing structures from the workspace, with extraction of their names from directory didn’t work out the same.
projectdir = 'C:\...\elements';
dinfo = dir( fullfile(projectdir, '*.mat') );
filenames = fullfile( {dinfo.name} );
nfiles = length(filenames);
cells = cell(nfiles, 1);
for i = 1:length(filenames)
cells{i} = load(filenames{i});
end
try
st = horzcat(cells{:});
catch ME
error('The mat files do not have identical variables in same order')
end
Andrew
Andrew on 17 Aug 2020
Edited: Andrew on 17 Aug 2020
Thx 4 hepl. I had to rearrange struct "documentation" style and use this script from blog...
cells{i} = orderfields(load(filenames{i})); %sorts fields in ASCII order

Sign in to comment.

More Answers (0)

Asked:

on 15 Aug 2020

Edited:

on 17 Aug 2020

Community Treasure Hunt

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

Start Hunting!