Save class object as struct

I want to automatically convert class objects to structs before saving them to MAT files.
This webpage seems to explain the task that I expect. However, when I try the following, it trurn out that it is not as expected:
mySuper = MySuper;
save('m.mat', 'mySuper')
load('m.mat')
The saved file contains mySuper variable still as a class object, not a struct.
If I delete the class and load the file afterwards, I get:
Warning: Variable 'mySuper' originally saved as a MySuper cannot be instantiated as an object and will be read in as a uint32.
> In uiimport/runImportdata (line 459)
In uiimport/gatherFilePreviewData (line 438)
In uiimport (line 262)
Class MySuper could be extended by changing the saveobj method to the following:
function S = saveobj(obj)
S = struct(obj);
end
But this doesn't work either.
I'm using MATLAB R2020a.

4 comentarios

Steven Lord
Steven Lord el 20 de Mayo de 2021
I want to automatically convert class objects to structs before saving them to MAT files.
Why? If you do that then when you load the data from the MAT-file it will no longer be an instance of that class and you will be unable to call methods of that class on it.
Behnam
Behnam el 20 de Mayo de 2021
The reason is that I want my saved MAT files to be independent. If I change the class name/properties or if I remove the class, I want to be able to still read the MAT files.
The trick introduced in the same webpage allows to reconstruct the class instance.
Behnam
Behnam el 11 de Jun. de 2021
Any other comments on this issue? I believe the behavior reported in this post is not the expected behavior of saveobj, as explained in this page.
Michael Jablecki
Michael Jablecki el 23 de Jun. de 2021
This has been a problem for a while. Object serialization tends to have issues, and I have had parallel problems with Python pickled objects. It is a portability problem. When the class changes, the saved object will no longer be able to be loaded in many cases, making debugging challenging if the process involves saved (serialized) instances separated from the code which generated them. I'd stick with saving as structures and reconstitute as described above rather than ever saving a "native class instance". They are trouble.

Iniciar sesión para comentar.

 Respuesta aceptada

J. Alex Lee
J. Alex Lee el 11 de Jun. de 2021

1 voto

I think it means when you implement saveobj/loadobj, it will internally use a struct intermediary, but ultimately when you load the object, the intent is that the output will be an object, not a struct.
if need the struct, I guess you can just do
s = struct(obj)
save("somepath.mat",'s')
or just wrap that in a "export" method for your class.

3 comentarios

Behnam
Behnam el 28 de Jul. de 2021
I am not sure if it really uses a struct intermediary. I would expect to get a struct by loading the mat file after deleting the class. However, I get a warning that the variable was saved as an object, i.e., as a MySuper.
This behavior is the same in MATLAB 2020b and even without a loadobj method.
J. Alex Lee
J. Alex Lee el 28 de Jul. de 2021
And I am saying the docs suggest that you can now be sure it does use a struct intermediary - the load/saveobj methods are not meant to save your object as structs, but rather to give you granularity on exactly how you design that struct intermediary.
Matt J
Matt J el 28 de Jul. de 2021
Yes, the loadobj/saveobj mechanism is the intended way to handle saved objects that are out of date with their class definitions.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Data Type Identification en Centro de ayuda y File Exchange.

Productos

Versión

R2020a

Preguntada:

el 20 de Mayo de 2021

Comentada:

el 28 de Jul. de 2021

Community Treasure Hunt

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

Start Hunting!

Translated by