Got Questions? Get Answers.
Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Saving and loading class objects

Subject: Saving and loading class objects

From: Adrian

Date: 29 Sep, 2009 16:15:20

Message: 1 of 12

I just started working with classes in matlab and need some help.
I have created an object (testECG) that is a specific instance of my class (ecg). I want to save this object and some other variables to a .mat file and load them later. I can load the .mat without problem before I close Matlab. However, after I close matlab and reopen I get the following error about my object:

Warning: Variable 'testECG' originally saved as a ecg cannot be instantiated
as an object and will be read in as a uint32.

All my other variables load fine but the object is clearly missing its appropriate values. So basically how do a save and load the specific class object that was created?

Subject: Saving and loading class objects

From: Steven Lord

Date: 29 Sep, 2009 16:59:50

Message: 2 of 12


"Adrian " <adrian.suszko@uhn.on.ca> wrote in message
news:h9tbqo$q5f$1@fred.mathworks.com...
>I just started working with classes in matlab and need some help.
> I have created an object (testECG) that is a specific instance of my class
> (ecg). I want to save this object and some other variables to a .mat file
> and load them later. I can load the .mat without problem before I close
> Matlab. However, after I close matlab and reopen I get the following error
> about my object:
>
> Warning: Variable 'testECG' originally saved as a ecg cannot be
> instantiated
> as an object and will be read in as a uint32.
>
> All my other variables load fine but the object is clearly missing its
> appropriate values. So basically how do a save and load the specific class
> object that was created?

My guess is that the directory containing the class definition (classdef)
file or the @testECG class directory is not on the path and so the
definition for how to construct an object of the class is not available when
you try to load it. To solve this, add the directory containing the
classdef file or the @testECG directory to the path, then load.

--
Steve Lord
slord@mathworks.com
comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ

Subject: Saving and loading class objects

From: Adrian

Date: 2 Oct, 2009 16:21:02

Message: 3 of 12

"Steven Lord" <slord@mathworks.com> wrote in message <h9tedf$k6s$1@fred.mathworks.com>...
>
> "Adrian " <adrian.suszko@uhn.on.ca> wrote in message
> news:h9tbqo$q5f$1@fred.mathworks.com...
> >I just started working with classes in matlab and need some help.
> > I have created an object (testECG) that is a specific instance of my class
> > (ecg). I want to save this object and some other variables to a .mat file
> > and load them later. I can load the .mat without problem before I close
> > Matlab. However, after I close matlab and reopen I get the following error
> > about my object:
> >
> > Warning: Variable 'testECG' originally saved as a ecg cannot be
> > instantiated
> > as an object and will be read in as a uint32.
> >
> > All my other variables load fine but the object is clearly missing its
> > appropriate values. So basically how do a save and load the specific class
> > object that was created?
>
> My guess is that the directory containing the class definition (classdef)
> file or the @testECG class directory is not on the path and so the
> definition for how to construct an object of the class is not available when
> you try to load it. To solve this, add the directory containing the
> classdef file or the @testECG directory to the path, then load.
>
> --
> Steve Lord
> slord@mathworks.com
> comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ
>
They already where a part of my path but I moved the ecg class into a new folder just for organization purposes, added that folder to the path and now everything seemingly works. Strange.

Subject: Saving and loading class objects

From: ?yvind

Date: 10 Dec, 2009 10:59:18

Message: 4 of 12

"Adrian " <adrian.suszko@uhn.on.ca> wrote in message <h9tbqo$q5f$1@fred.mathworks.com>...
> I just started working with classes in matlab and need some help.
> I have created an object (testECG) that is a specific instance of my class (ecg). I want to save this object and some other variables to a .mat file and load them later. I can load the .mat without problem before I close Matlab. However, after I close matlab and reopen I get the following error about my object:
>
> Warning: Variable 'testECG' originally saved as a ecg cannot be instantiated
> as an object and will be read in as a uint32.
>
> All my other variables load fine but the object is clearly missing its appropriate values. So basically how do a save and load the specific class object that was created?


But why are the objects loaded as uint32 in the first place?! They used to be loaded as structs. How does one get at the data in the case that one doesn't have access to the class definition? When they were loaded as structs, at least you could salvage most of the information. I have also experieced that when the classdef has changed too much, the object is loaded as a uint32, and I'm completely helpless to get back my data.

In some cases the objects aren't even loaded as uint32, you get an error 'Can't read file' instead.

This is very problematic in my opinion.

Subject: Saving and loading class objects

From: Steven Lord

Date: 10 Dec, 2009 14:50:45

Message: 5 of 12


"?yvind " <oyvist@gmail.com> wrote in message
news:hfqka6$41e$1@fred.mathworks.com...
> "Adrian " <adrian.suszko@uhn.on.ca> wrote in message
> <h9tbqo$q5f$1@fred.mathworks.com>...
>> I just started working with classes in matlab and need some help.
>> I have created an object (testECG) that is a specific instance of my
>> class (ecg). I want to save this object and some other variables to a
>> .mat file and load them later. I can load the .mat without problem before
>> I close Matlab. However, after I close matlab and reopen I get the
>> following error about my object:
>>
>> Warning: Variable 'testECG' originally saved as a ecg cannot be
>> instantiated
>> as an object and will be read in as a uint32.
>>
>> All my other variables load fine but the object is clearly missing its
>> appropriate values. So basically how do a save and load the specific
>> class object that was created?

Make sure the class definition file is available to MATLAB at the time the
object is loaded, so that MATLAB can determine what properties the object
should have.

> But why are the objects loaded as uint32 in the first place?! They used to
> be loaded as structs. How does one get at the data in the case that one
> doesn't have access to the class definition? When they were loaded as
> structs, at least you could salvage most of the information. I have also
> experieced that when the classdef has changed too much, the object is
> loaded as a uint32, and I'm completely helpless to get back my data.

If the class definition file has changed so that the old objects are no
longer valid instances of the class (say you've deleted or changed the
attributes for a property [adding Dependent to one, perhaps]), you could
write a LOADOBJ method for the class that knows how to handle the older
objects and convert them into instances of the new definition.

http://www.mathworks.com/access/helpdesk/help/techdoc/matlab_oop/br0lx_g.html

> In some cases the objects aren't even loaded as uint32, you get an error
> 'Can't read file' instead.

That sounds suspicious to me. I'd contact Technical Support with an example
of such a file and the class definition file for the object that you're
trying to load from it.

--
Steve Lord
slord@mathworks.com
comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ

Subject: Saving and loading class objects

From: Matt J

Date: 10 Dec, 2009 16:31:22

Message: 6 of 12

"Steven Lord" <slord@mathworks.com> wrote in message <hfr1pa$2nd$1@fred.mathworks.com>...
>
> "?yvind " <oyvist@gmail.com> wrote in message
> news:hfqka6$41e$1@fred.mathworks.com...
> > "Adrian " <adrian.suszko@uhn.on.ca> wrote in message
> > <h9tbqo$q5f$1@fred.mathworks.com>...
> >> I just started working with classes in matlab and need some help.
> >> I have created an object (testECG) that is a specific instance of my
> >> class (ecg). I want to save this object and some other variables to a
> >> .mat file and load them later. I can load the .mat without problem before
> >> I close Matlab. However, after I close matlab and reopen I get the
> >> following error about my object:
> >>
> >> Warning: Variable 'testECG' originally saved as a ecg cannot be
> >> instantiated
> >> as an object and will be read in as a uint32.
> >>
> >> All my other variables load fine but the object is clearly missing its
> >> appropriate values. So basically how do a save and load the specific
> >> class object that was created?
>
> Make sure the class definition file is available to MATLAB at the time the
> object is loaded, so that MATLAB can determine what properties the object
> should have.
>
> > But why are the objects loaded as uint32 in the first place?! They used to
> > be loaded as structs. How does one get at the data in the case that one
> > doesn't have access to the class definition? When they were loaded as
> > structs, at least you could salvage most of the information. I have also
> > experieced that when the classdef has changed too much, the object is
> > loaded as a uint32, and I'm completely helpless to get back my data.
>
> If the class definition file has changed so that the old objects are no
> longer valid instances of the class (say you've deleted or changed the
> attributes for a property [adding Dependent to one, perhaps]), you could
> write a LOADOBJ method for the class that knows how to handle the older
> objects and convert them into instances of the new definition.


I'm still puzzled about this uint32 thing.

If MATLAB has a way of converting an invalid saved object to type struct, for the purpose of passing it to loadobj, why would it not simply load the object as a struct into the workspace as a default loadobj procedure? Why would it be useful to convert it to uint32?

Subject: Saving and loading class objects

From: ?yvind

Date: 10 Dec, 2009 16:34:22

Message: 7 of 12

"Steven Lord" <slord@mathworks.com> wrote in message

> > But why are the objects loaded as uint32 in the first place?! They used to
> > be loaded as structs. How does one get at the data in the case that one
> > doesn't have access to the class definition? When they were loaded as
> > structs, at least you could salvage most of the information. I have also
> > experieced that when the classdef has changed too much, the object is
> > loaded as a uint32, and I'm completely helpless to get back my data.
>
> If the class definition file has changed so that the old objects are no
> longer valid instances of the class (say you've deleted or changed the
> attributes for a property [adding Dependent to one, perhaps]), you could
> write a LOADOBJ method for the class that knows how to handle the older
> objects and convert them into instances of the new definition.

Yes, but if the loadobj method is only given a uint32 vector, there isn't much for it to work with. My question is, does anybody know why unrecognized objects are no longer loaded as structs, but as uint32 instead? If I don't have access to the classdef file, there's no way to get at the data anymore.

> > In some cases the objects aren't even loaded as uint32, you get an error
> > 'Can't read file' instead.
>
> That sounds suspicious to me. I'd contact Technical Support with an example
> of such a file and the class definition file for the object that you're
> trying to load from it.

Just wanted to hear if anyone here had any ideas, first.

Thanks.

Subject: Saving and loading class objects

From: Matt J

Date: 10 Dec, 2009 17:06:21

Message: 8 of 12

"?yvind " <oyvist@gmail.com> wrote in message <hfr7ue$mtj$1@fred.mathworks.com>...

>My question is, does anybody know why unrecognized objects are no longer loaded as structs, but as uint32 instead?
==============

I think they still are loaded as structs into the workspace of loadobj (assuming you've defined a loadobj method).

If you haven't defined one, I guess it's an open question why they wouldn't be loaded into the base workspace as a struct by default.

Subject: Saving and loading class objects

From: ?yvind

Date: 11 Dec, 2009 14:02:19

Message: 9 of 12

"Matt J " <mattjacREMOVE@THISieee.spam> wrote in message <hfr9qd$p49$1@fred.mathworks.com>...
> "?yvind " <oyvist@gmail.com> wrote in message <hfr7ue$mtj$1@fred.mathworks.com>...
>
> >My question is, does anybody know why unrecognized objects are no longer loaded as structs, but as uint32 instead?
> ==============
>
> I think they still are loaded as structs into the workspace of loadobj (assuming you've defined a loadobj method).
>
> If you haven't defined one, I guess it's an open question why they wouldn't be loaded into the base workspace as a struct by default.

That's possible, I guess. But without access to the classdef file, you would need to define a mock version of the class with a loadobj method, and go from there. Seems unnecessarily complicated.

Subject: Saving and loading class objects

From: Matt J

Date: 11 Dec, 2009 15:03:04

Message: 10 of 12

"?yvind " <oyvist@gmail.com> wrote in message <hftjda$rvd$1@fred.mathworks.com>...
> "Matt J " <mattjacREMOVE@THISieee.spam> wrote in message <hfr9qd$p49$1@fred.mathworks.com>...
> > "?yvind " <oyvist@gmail.com> wrote in message <hfr7ue$mtj$1@fred.mathworks.com>...
> >
> > >My question is, does anybody know why unrecognized objects are no longer loaded as structs, but as uint32 instead?
> > ==============
> >
> > I think they still are loaded as structs into the workspace of loadobj (assuming you've defined a loadobj method).
> >
> > If you haven't defined one, I guess it's an open question why they wouldn't be loaded into the base workspace as a struct by default.
>
> That's possible, I guess. But without access to the classdef file, you would need to define a mock version of the class with a loadobj method, and go from there. Seems unnecessarily complicated.
====================

Don't really follow that. The loadobj method would typically call the constructor with input arguments taken from the struct. It would then return an object consistent with the classdef file.

Subject: Saving and loading class objects

From: ?yvind

Date: 11 Dec, 2009 15:17:02

Message: 11 of 12

"Matt J " <mattjacREMOVE@THISieee.spam> wrote in message <hftmv7$cfp$1@fred.mathworks.com>...
> "?yvind " <oyvist@gmail.com> wrote in message <hftjda$rvd$1@fred.mathworks.com>...
> > "Matt J " <mattjacREMOVE@THISieee.spam> wrote in message <hfr9qd$p49$1@fred.mathworks.com>...
> > > "?yvind " <oyvist@gmail.com> wrote in message <hfr7ue$mtj$1@fred.mathworks.com>...
> > >
> > > >My question is, does anybody know why unrecognized objects are no longer loaded as structs, but as uint32 instead?
> > > ==============
> > >
> > > I think they still are loaded as structs into the workspace of loadobj (assuming you've defined a loadobj method).
> > >
> > > If you haven't defined one, I guess it's an open question why they wouldn't be loaded into the base workspace as a struct by default.
> >
> > That's possible, I guess. But without access to the classdef file, you would need to define a mock version of the class with a loadobj method, and go from there. Seems unnecessarily complicated.
> ====================
>
> Don't really follow that. The loadobj method would typically call the constructor with input arguments taken from the struct. It would then return an object consistent with the classdef file.

Say I'm at a different computer, I don't have my code, including the classdef-files. I want to load the mat-file anyway (it's stored on some server) and have a look at the contents. If I try to do that now I'll have to write up a new version of the classdef file with a loadobj method in order to get at the data.

Loading the object as a struct as default would save you in cases were stuff just goes wrong.

Subject: Saving and loading class objects

From: Matt J

Date: 11 Dec, 2009 16:24:04

Message: 12 of 12

"?yvind " <oyvist@gmail.com> wrote in message <hftnpe$5ik$1@fred.mathworks.com>...

> > Don't really follow that. The loadobj method would typically call the constructor with input arguments taken from the struct. It would then return an object consistent with the classdef file.
>
> Say I'm at a different computer, I don't have my code, including the classdef-files. I want to load the mat-file anyway (it's stored on some server) and have a look at the contents. If I try to do that now I'll have to write up a new version of the classdef file with a loadobj method in order to get at the data.
>
> Loading the object as a struct as default would save you in cases were stuff just goes wrong.
=================

OK, I see what you mean. And you say it was possible to load as struct in older versions of MATLAB?

Of course, it's philosophically a bit odd to allow one to load a data type when the definition of the data type hasn't been installed on the machine. It would be somewhat like trying to load a plain double variable from a .mat file when MATLAB wasn't even installed.

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us