Removing top layer of struct

45 次查看(过去 30 天)
Jonathan
Jonathan 2016-3-7
编辑: Stephen23 2022-11-25
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 个评论
Stephen23
Stephen23 2022-11-25
编辑:Stephen23 2022-11-25
"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:

请先登录,再进行评论。

回答(2 个)

Geoff Hayes
Geoff Hayes 2016-3-7
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 2017-11-23
编辑:Chieh-Hsun Wu 2017-11-23
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 个评论
BINGXIN YAN
BINGXIN YAN 2022-11-25
编辑:BINGXIN YAN 2022-11-25
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 2022-11-25
"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:

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Structures 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by