Failure to load structures into an array through a loop

I have a series of structures stored as .mat files in different folders. The path of these files are provided to me via the "path" column in a table named "Table_Feed". I want to load these structs into Matlab and store them in an array called "Store_Data" for further processing:
for i = total_count:-1:1
Store_Data(i) = load(Table_Feed{i,'path'});
end
The error is:
Subscripted assignment between dissimilar structures.
I tried to check the integrity of the path and .mat files as:
load(Table_Feed{1,'path'});
It worked fine and the struct was inserted into my workspace; I tried for all values of i <= total_count. I would appreciate help on where I went wrong.

4 Comments

"I would appreciate help on where I went wrong."
Apparently some of the MAT files contain variables with different names, in which case they cannot be simply assigned to the same structure:
S = struct('x',pi)
S = struct with fields:
x: 3.1416
S(2) = struct('y',Inf)
Subscripted assignment between dissimilar structures.
This is very unfortunate data design: if possible, keep the variable names the same in all files, then your code will be simpler and more robust. Otherwise, you will have to use e.g. a cell array to store them, just as Bruno Luong showed.
In each loop-run, I am loading a 1*1 struct with 1 field. This field has subfields (of various kinds from string to table) with respective values. The names and types are always same.
"The names and types are always same"
As I showed in my earlier comment, you are getting that error message because you are attempting to concatenate two structures together, which have different fieldnames. Where those structures come from and why they have different fieldnames is not something that I can tell you: that is your task to debug, because you have that data and can run your code, I cannot (unless you upload everything here required to run the code).
For a start, you should look at whether that structure exists already in the workspace, and also check the content of the MAT files. You need to investigate what really is happening, not just rely on what you think is happening.
Type
dbstop if error
run your code, when it breaks with the error, type
Store_Data
and
sload = load(Table_Feed{i,'path'})
and report the results (you'll see why the structures are dissimilar).

Sign in to comment.

 Accepted Answer

I copy my answer fromthis thread
Usage is typically like this:
cellresult = cell(1,n)
for i=1:n
% do something first
% ...
% call iteration subtask that returns a structure or structure array
cellresult{i} = load(Table_Feed{i,'path'});
% do something else
% ...
end
dim = 2; % whatever elongation of structresult you want to get
structresult = catstruct(dim, cellresult); % function mfiles attached
The function CATSTRUCTS can deal with a list of structures that are all dissimilar, so very generic possible usage.

More Answers (0)

Categories

Products

Release

R2022b

Community Treasure Hunt

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

Start Hunting!