Save class object as struct
125 ビュー (過去 30 日間)
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;
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);
But this doesn't work either.
I'm using MATLAB R2020a.
J. Alex Lee 2021 年 6 月 11 日
Look at this too: https://www.mathworks.com/help/matlab/matlab_oop/flexible-saveobj-and-loadobj-pattern.html
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)
or just wrap that in a "export" method for your class.