Removing top layer of struct

52 views (last 30 days)
Jonathan
Jonathan on 7 Mar 2016
Edited: Stephen23 on 25 Nov 2022
Hi,
When I load a struct I've previously saved to a .mat file, I've specified a generic name for the struct before, say fileStruct. I'd like to open the struct via load, and have the name of the struct be more specific, like "fileStructA", but when I try fileStructA=load(.....,fileStruct), I get a struct back whose name is fileStructA, with the next level down still named "fileStruct". Can I avoid this happening, and just get a struct fileStructA without that second redundant level? If not, what's the easiest way to remove the top layer of a struct? Do I just have to simply define a new struct?
Cheers
  1 Comment
Stephen23
Stephen23 on 25 Nov 2022
Edited: Stephen23 on 25 Nov 2022
"When I load a struct I've previously saved to a .mat file, I've specified a generic name for the struct before, say fileStruct. I'd like to open the struct via load, and have the name of the struct be more specific, like "fileStructA""
So far no one has mentioned the likely cause of this all: forcing meta-data into the structure name.
This situation commonly occurs when beginners import MAT files in a loop and overwrite the imported data on each iteration... so they imagine that the only solution is to rename the imported data variable on each loop iteration, usually wanting "the same name as the file" or some such similar idea. This approach is https://en.wikipedia.org/wiki/Anti-pattern
In fact code will be simpler, more efficient, and much more robust when the names (either in the files or in the workspace) do not change on each loop iteration, and instead use simple indexing on each iteration, just like the MATLAB documentation shows:

Sign in to comment.

Answers (2)

Geoff Hayes
Geoff Hayes on 7 Mar 2016
Jonathan - the behaviour that you are observing seems reasonable (and intended) given that there could be more than one variable in the mat file. I don't think that you can get around this issue.
The easiest way to "strip off" the top layer struct is to do something like
fileStructA = load('myData.mat');
fileStructA = fileStructA.fileStruct;

Chieh-Hsun Wu
Chieh-Hsun Wu on 23 Nov 2017
Edited: Chieh-Hsun Wu on 23 Nov 2017
Hello Janathan,
Here is my way of doing it:
load('myData.mat');%Load the structure file 'MyStructure'
Names = fieldnames(MyStructure);%List all variables under MyStructure
for nn = 1:length(Names)
eval([Names{nn},' = MyStructure.',Names{nn},';']);% Assign data to original names
end
Hope this works. CHW
  4 Comments
BINGXIN YAN
BINGXIN YAN on 25 Nov 2022
Edited: BINGXIN YAN on 25 Nov 2022
I think the reason is like this. Consider the scenario, we want to define a function with a large number of input variables. We may want to save the input variables in a struct for convenience. We then set the struct_input as input of our function. The code might be like this:
struct_input = struct()
struct_input.x = x
struct_input.y = y
struct_input.z = z
struct_input.a = a
struct_input.b = b
struct_input.c = c
Under the function, we may need to "release" the input variables from the struct_input one-by-one, like this
[val] = function(struct_input):
x = struct_input.x
y = struct_input.y
z = struct_input.z
a = struct_input.a
b = struct_input.b
c = struct_input.c
val = x*y+a*z+exp(b*c)
end
The deficiency is obvious, we need to code 2*length(struct_input) times for the input variables.
BUT, with the above code, we can change the code to this
[val] = function(struct_input):
Names = fieldnames(struct_input);
for nn = 1:length(Names)
eval([Names{nn},' = struct_input.',Names{nn},';']);
end
end
That is my understand.
Stephen23
Stephen23 on 25 Nov 2022
"The deficiency is obvious"
Yes: very inefficient, obfuscated code.
"we need to code 2*length(struct_input)"
Indicating confusion between the size of a structure and how many fields it has.
See also:

Sign in to comment.

Categories

Find more on Structures in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!