saving mat file changes my structures!

7 vues (au cours des 30 derniers jours)
Brad Rosenheim
Brad Rosenheim le 21 Juil 2015
Modifié(e) : Stephen23 le 28 Juin 2019
Hello -
I have a function that compiles data into a structure which consists of field: num which is a 1x24 cell. Thus, D.num = {1x24 cell} My function does this correctly, and allows me to interface with an existing function. However, when my function saves the structure, D, as a mat file, the mat file adds a field. I end up with: D.D.num = {1x24 cell]
I have probed the function and there is only one field when it runs. But the saving is what is changing it. I use the command: save('fname.mat','D') to save the matfile. When I do this from my workspace (and not within the program) it works fine. When the program (function) saves the file, it adds and extra field that is redundant. This is making further programming hard, and I don't see many other options to save this variable.
Thanks - Brad
  1 commentaire
Cedric
Cedric le 21 Juil 2015
Modifié(e) : Cedric le 21 Juil 2015
This is because SAVE offers the possibility to save multiple variables at once defined by their names (passed as strings):
>> a = 8 ; b = 9 ;
>> save( 'test.mat', 'a', 'b' ) ;
When we LOAD test.mat, in an ideal world where people would remember/know what was saved in their MAT-Files, MATLAB could assume that we would know that there were two variables, and build an output that we could use this way:
>> [a, b] = load( 'test.mat' ) ; % Wrong.
Yet, my guess is that MATLAB devs had to account for the fact that we usually don't know what we did, especially when someone else did it, so they preferred outputting a struct that contains the variables that were saved as fields:
>> data = load( 'test.mat' )
data =
a: 8
b: 9
What tricked you is that you named the output of LOAD D, so you thought that the struct had been kind of expanded like in Inception. If you name it differently though, like data, you'll see that it will contain D and the whole thing will make sense!
>> data = load( ... )
data =
D: [1x1 struct]
>> D = data.D % Extract field D which is the former variable D.
D =
num: {...}
Signed: someone who didn't understand why he got data.data after loading some MAT-File years ago.

Connectez-vous pour commenter.

Réponses (2)

Muthu Annamalai
Muthu Annamalai le 21 Juil 2015
Your problem maybe that you are loading wrong, whereas your save code seems OK.
Try this code for your load function,
function D = foo()
D = [];
load('fname.mat'); %this will load D in the local workspace.
return
end
which should overwrite the local copy D and return it to the caller.

Image Analyst
Image Analyst le 21 Juil 2015
You're probably doing
D = load('fname.mat');
Doing it this way will create a structure called D with all the variables you saved as fields off of that structure. So that's how you got a D.D. If you had also saved a myVar variable, you would get a D.myVar field.
Either read into a structure and pick off your variable from the structure:
storedStruct = load('fname.mat');
D = storedStructure.D;
or "poof" the variables into existence:
load('fname.mat');
I think the first method is the preferred one since people usually don't like to poof variables into workspaces.
  9 commentaires
MSani
MSani le 28 Juin 2019
I started my own question for my first issue and was asked to add it to an existing question. That is why I am doing the same now or else I would have started a new thread instead.
Stephen23
Stephen23 le 28 Juin 2019
Modifié(e) : Stephen23 le 28 Juin 2019

Connectez-vous pour commenter.

Catégories

En savoir plus sur Structures dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by