Reputation: 13
I am trying to save and load structure arrays in a MAT-file, but the structure array keeps changing every time I reload it. If save the following and reload it, it keeps adding struct in front.
struct.field1
struct.field2
save data.mat struct
struct = load('data.mat');
I know that this is happening because I load the file in a variable which makes it a struct and that it won't if I use only:
load('data.mat')
However I am calling the load command within a function and therefore I cannot use this. Does anyone have an idea how to solve this, so that I don't get:
struct.struct.struct.struct.struct.field1;
struct.struct.struct.struct.struct.field2;
after couple of times reloading the data.mat file, but just this:
struct.field1;
struct.field2;
Kind regards,
Romano
Upvotes: 0
Views: 1953
Reputation: 1693
To avoid adding deeper nested structs you could choose to save all fields as individual variables using the content option -struct
MystructName.field1 = 0
MystructName.field2 = 1
save('data.mat', '-struct', 'MystructName')
Then load the data to a variable and I'll see that the structure hasn't changed
MyStructName = load('data.mat')
MyStructName =
field1: 0
field2: 1
Ps. Perhaps this is only in your example, but naming your struct to struct
is bad since it overwrites the Matlab built-in function named struct
.
Upvotes: 1