Removing top layer of struct

조회 수: 27 (최근 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