Removing top layer of struct

27 visualizaciones (últimos 30 días)
Jonathan
Jonathan el 7 de Mzo. de 2016
Editada: Stephen23 el 25 de Nov. de 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 comentario
Stephen23
Stephen23 el 25 de Nov. de 2022
Editada: Stephen23 el 25 de Nov. de 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:

Iniciar sesión para comentar.

Respuestas (2)

Geoff Hayes
Geoff Hayes el 7 de Mzo. de 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 el 23 de Nov. de 2017
Editada: Chieh-Hsun Wu el 23 de Nov. de 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 comentarios
BINGXIN YAN
BINGXIN YAN el 25 de Nov. de 2022
Editada: BINGXIN YAN el 25 de Nov. de 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 el 25 de Nov. de 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:

Iniciar sesión para comentar.

Categorías

Más información sobre Structures en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by