saveobj - Customize save process for objects - MATLAB (original) (raw)
Main Content
Customize save process for objects
Syntax
Description
Note
The matlab.mixin.CustomElementSerialization class is recommended overloadobj
and saveobj
because the mixin provides greater control over how objects are serialized and deserialized, including the ability to add, delete, and rename properties. (since R2024b)
[b](#mw%5F3a0881b7-fdd4-4e8d-bf01-63785d4c4c9f) = saveobj([a](#mw%5F2ea8eba9-76fa-465c-9598-90f83415b641))
is called by the save function if the class ofa
defines a saveobj
method.save
writes the returned value, b
, to a MAT file.
Examples
The ContactEntry
class defines three properties. The class defines asaveobj
method that saves those property values in a structure, and aloadobj
method that recreates the object from the saved structure.
classdef ContactEntry properties Name Email Cell end methods function s = saveobj(obj) s.Name = obj.Name; s.Email = obj.Email; s.Cell = obj.Cell; end end methods (Static) function obj = loadobj(s) if isstruct(s) newObj = ContactEntry; newObj.Name = s.Name; newObj.Email = s.Email; newObj.Cell = s.Cell; obj = newObj; else obj = s; end end end end
Create an instance of ContactEntry
and save it.
a = ContactEntry; a.Name = "Sandy Trent"; a.Email = "strent@notacompany.com"; a.Cell = "617-555-1212"; save("C:_yourpath_\ContactFile.mat","a");
Revise ContactEntry
to include a new property that holds a work phone number.
classdef ContactEntry properties Name Email Cell WorkPhone end properties (Transient,Hidden) SaveInOldFormat = false; end methods function s = saveobj(obj) s.Name = obj.Name; s.Email = obj.Email; s.Cell = obj.Cell; if ~obj.SaveInOldFormat s.WorkPhone = obj.WorkPhone; end end end methods (Static) function obj = loadobj(s) if isstruct(s) newObj = ContactEntry; newObj.Name = s.Name; newObj.Email = s.Email; newObj.Cell = s.Cell; if numel(fieldnames(s)) == 4 newObj.WorkPhone = s.WorkPhone; else newObj.WorkPhone = "unknown"; end obj = newObj; else obj = s; end end end end
To maintain compatibility between the two class versions:
- The
loadobj
method has been modified to check for a fourth field in the serialized structure. If there is a fourth field,loadobj
sets theWorkPhone
property to that value. If not, it sets theWorkPhone
property to"unknown"
. - A new transient, hidden property,
SaveInOldFormat
, enables class users to specify whether they want to serialize an instance of the class in the old format, without theWorkPhone
property value. Thesaveobj
method only saves theWorkPhone
value ifSaveInOldFormat
isfalse
.
Clear the existing instance a
from memory, and loada
under the new definition of the class. The revisedloadobj
method recognizes the saved data as being from the old version of the class, and sets WorkPhone
appropriately.
clear a load("C:_yourpath_\ContactFile.mat","a") a
a =
ContactEntry with properties:
Name: "Sandy Trent"
Email: "strent@notacompany.com"
Cell: "617-555-1212"
WorkPhone: "unknown"
Likewise, you can create a new instance of the class with a value ofWorkPhone
, and both the saveobj
andloadobj
methods save and load the value as expected under the new class definition.
Input Arguments
Output Arguments
Data passed to save
function by MATLABĀ®, which can be:
- An object
- Property names and current values stored in a structure
Tips
- Implement your
saveobj
method to work with scalar objects or structures. When you save an object array,save
callssaveobj
on each element of the array.
Version History
Introduced before R2006a