Removing top layer of struct
26 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
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 Kommentar
Stephen23
am 25 Nov. 2022
Bearbeitet: Stephen23
am 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:
Antworten (2)
Geoff Hayes
am 7 Mär. 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;
0 Kommentare
Chieh-Hsun Wu
am 23 Nov. 2017
Bearbeitet: Chieh-Hsun Wu
am 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 Kommentare
BINGXIN YAN
am 25 Nov. 2022
Bearbeitet: BINGXIN YAN
am 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
am 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:
Siehe auch
Kategorien
Mehr zu Structures finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!