matlab.serialization.ElementSerializationContent Class
Namespace: matlab.serialization
Superclasses: matlab.mixin.CustomDisplay
, matlab.mixin.indexing.RedefinesDot
, handle
Description
A matlab.serialization.ElementSerializationContent
instance represents an
object to be serialized or deserialized.
matlab.serialization.ElementSerializationContent
implements methods that add,
remove, or change the properties that are saved or loaded.
The matlab.serialization.ElementSerializationContent
class is a handle
class.
Creation
matlab.serialization.ElementSerializationContent
objects are created by the
modifyOutgoingSerializationContent
and
modifyIncomingSerializationContent
methods of matlab.mixin.CustomElementSerialization
. You cannot instantiate
matlab.serialization.ElementSerializationContent
directly.
Methods
Public Methods
addNameValue |
Add a property name and its value to an object being serialized or deserialized. Input Arguments
If you want to add a private property, qualify the
name with the owning class. In other words, the |
addDynamicNameValue |
Add a dynamic property name and its value to an object being serialized or deserialized. Input Arguments
|
getValue |
Retrieve the value of a property from an object being serialized or deserialized. Input Arguments
|
hasNameValue |
Returns a
logical Input Arguments
|
remove |
Remove a property from an object being serialized or deserialized. Input Arguments
|
rename |
Rename a property of the object being serialized or deserialized. The value of the property remains the same. Input Arguments
If you rename a property and also want that to change
that property to private, qualify the name with the owning class. In other words,
the |
serializedPropertyNames |
Returns a string array of
property names defined in an object being serialized or deserialized. Use
Input Arguments
|
updateValue |
Update the value of a property of an object being serialized or deserialized. Input Arguments
|
Examples
Load Serialized Objects After Changing Class Definition
Define a class Rectangle
that represents a rectangle on the
coordinate plane. The properties define the coordinates of the lower-left corner
(X
and Y
) and the width and height of the
rectangle.
classdef Rectangle properties X Y Width Height end methods function obj = Rectangle(x,y,w,h) obj.X = x; obj.Y = y; obj.Width = w; obj.Height = h; end end end
Create an instance of Rectangle
and save it. Replace
yourfilepath
with your local path.
rOld = Rectangle(1,1,4,5); save("yourfilepath/oldRectangle.mat","rOld");
Revise the class definition of Rectangle
so that the constructor
takes the same values, but the properties now define the lower-left corner of the
rectangle (X1
and Y1
) and the upper-right
corner (X2
and Y2
). To be able to load objects
saved under the old definition, make these additional changes to the class definition:
Inherit from
matlab.mixin.CustomElementSerialization
.Implement the static method
modifyIncomingSerializationContent
. This method is called whenever you attempt to load aRectangle
object. (For more information, seematlab.mixin.CustomElementSerialization
.)As the first step of
modifyIncomingSerializationContent
, callhasNameValue
on the serialized object,sObj
, which is an instance ofmatlab.serialization.ElementSerializationContent
. If the object has a property namedX
, the object was created using the old definition ofRectangle
, andmodifyIncomingSerializationContent
modifies the properties of the object to match the new definition.To update an instance serialized under the old definition,
modifyIncomingSerializationContent
calls four more methods ofmatlab.serialization.ElementSerializationContent
to update the object:rename
— Rename theX
andY
properties of the old class definition toX1
andY1
.getValue
— Retrieve the values ofWidth
andX
from the old object and add them to find the value ofX2
. Do the same forHeight
andY
.addNameValue
— Add the new propertiesX2
andY2
and their values.remove
— Remove the old propertiesWidth
andHeight
because they are not part of the new class definition.
classdef Rectangle < matlab.mixin.CustomElementSerialization properties X1 Y1 X2 Y2 end methods function obj = Rectangle(x,y,w,h) obj.X1 = x; obj.Y1 = y; obj.X2 = w + x; obj.Y2 = h + y; end end methods (Static) function modifyIncomingSerializationContent(sObj) if sObj.hasNameValue("X") sObj.rename("X","X1"); sObj.rename("Y","Y1"); newX2 = sObj.getValue("Width") + sObj.getValue("X1"); sObj.addNameValue("X2",newX2); newY2 = sObj.getValue("Height") + sObj.getValue("Y1"); sObj.addNameValue("Y2",newY2); sObj.remove("Width"); sObj.remove("Height"); end end end end
Clear rOld
from memory.
clear("rOld")
Remove the old definition of Rectangle
from your path. Add the
revised definition of Rectangle
to the path, and load the
rOld
object. Display the object to confirm that the variable has
been deserialized as an object of the revised definition of
Rectangle
.
load("yourfilepath/oldRectangle.mat","rOld"); rOld
rOld = Rectangle with properties: X1: 1 Y1: 1 X2: 5 Y2: 6
Note
This example shows how to revise a class definition for backward compatibility, so
that objects serialized under the old definition can be deserialized under the new
definition. For an example that shows backward and forward compatibility, see matlab.mixin.CustomElementSerialization
.
Manage Dynamic Properties During Serialization
The matlab.mixin.CustomElementSerialization
class enables you to add
dynamic properties to an instance of
matlab.serialization.ElementSerializationContent
. Define a class
DynDemo
that defines two properties, Prop1
and
TempData
.
classdef DynDemo properties Prop1 TempData end methods function obj = DynDemo(p,t) if nargin > 0 obj.Prop1 = p; obj.TempData = t; end end end end
Create an instance of DynDemo
and save it. Replace
yourfilepath
with your local path.
d = DynDemo([1:10],14); save("yourfilepath/oldDynDemo.mat","d");
Revise the class definition so that TempData
is now a dynamic property.
Inherit from
dynamicprops
andmatlab.mixin.CustomElementSerialization
.Implement the static method
modifyIncomingSerializationContent
. This method is called whenever you attempt to load aDynDemo
object. (For more information, seematlab.mixin.CustomElementSerialization
.)As the first step of
modifyIncomingSerializationContent
, callserializedPropertyNames("dynamic")
on the serialized object,sObj
, which is an instance ofmatlab.serialization.ElementSerializationContent
. If the object has no dynamic properties, the object was created using the old definition of theDynDemo
class. Remove the nondynamic version ofTempData
and add it as a dynamic property.If the object does have a dynamic property, the object was serialized under the new definition, and the object can be deserialized without any additional action.
classdef DynDemo < dynamicprops & matlab.mixin.CustomElementSerialization properties Prop1 end methods function obj = DynDemo(p) if nargin == 1 obj.Prop1 = p; end end end methods (Static) function modifyIncomingSerializationContent(sObj) if isempty(sObj.serializedPropertyNames("dynamic")) val = sObj.TempData; sObj.remove("TempData"); sObj.addDynamicNameValue("TempData",val); end end end end
Clear d
from memory.
clear("d")
Remove the old definition of DynDemo
from your path. Add the revised
definition of DynDemo
to the path, and load d
.
load("yourfilepath/oldDynDemo.mat","d");
Use findprop
to confirm that TempData
is now a
dynamic property.
findprop(d,"TempData")
ans = DynamicProperty with properties: Name: 'TempData' ...
Version History
Introduced in R2024b
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)