MATLAB Function Reference    

Save an object to a MAT-file



B = saveobj(A) is called by the MATLAB save function when object, A, is saved to a .MAT file. This call executes the saveobj method for the object's class, if such a method exists. The return value B is subsequently used by save to populate the .MAT file.

When you issue a save command on an object, MATLAB looks for a method called saveobj in the class directory. You can overload this method to modify the object before the save operation. For example, you could define a saveobj method that saves related data along with the object.


saveobj can be overloaded only for user objects. save will not call saveobj for a built-in datatype, such as double, even if @double/saveobj exists.

saveobj will be separately invoked for each object to be saved.

A child object does not inherit the saveobj method of its parent class. To implement saveobj for any class, including a class that inherits from a parent, you must define a saveobj method within that class directory.


The following example shows a saveobj method written for the portfolio class. The method determines if a portfolio object has already been assigned an account number from a previous save operation. If not, saveobj calls getAccountNumber to obtain the number and assigns it to the account_number field. The contents of b is saved to the MAT-file.

See Also

save, load, loadobj

  save (COM) save (serial)