This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English verison of the page.

Note: This page has been translated by MathWorks. Please click here
To view all translated materals including this page, select Japan from the country navigator on the bottom of this page.

Properties Containing Objects

Assigning Objects as Default Property Values

MATLAB® evaluates property default values only once when loading the class. MATLAB does not reevaluate the assignment each time you create an object of that class. If you assign an object as a default property value in the class definition, MATLAB calls the constructor for that object only once when loading the class.


Evaluation of property default values occurs only when the value is first needed, and only once when MATLAB first initializes the class. MATLAB does not reevaluate the expression each time you create an instance of the class.

For more information on the evaluation of expressions that you assign as property default values, see When MATLAB Evaluates Expressions.

Assigning to Read-Only Properties Containing Objects

When a class defines a property with private or protected SetAccess, and that property contains an object which itself has properties, assignment behavior depends on whether the property contains a handle or a value object:

  • Handle object – you can set properties on handle objects contained in read-only properties

  • Value object – you cannot set properties on value object contained in read-only properties.

Assignment Behavior

These classes illustrate the assignment behavior:

  • ReadOnlyProps – class with two read-only properties. The class constructor assigns a handle object of type HanClass to the PropHandle property and a value object of type ValClass to the PropValue property.

  • HanClass – handle class with public property

  • ValClass – value class with public property

classdef ReadOnlyProps
   properties(SetAccess = private)
      function obj = ReadOnlyProps
         obj.PropHandle = HanClass;
         obj.PropValue = ValClass;

classdef HanClass < handle

classdef ValClass

Create an instance of the ReadOnlyProps class:

a = ReadOnlyProps
a = 

 ReadOnlyProps with properties:

    PropHandle: [1x1 HanClass]
     PropValue: [1x1 ValClass]

Use the private PropHandle property to set the property of the HanClass object it contains:

ans =

a.PropHandle.Hprop = 7;

Attempting to make an assignment to the value class object property is not allowed:

a.PropValue.Vprop = 11;

You cannot set the read-only property 'PropValue' of ReadOnlyProps.

Related Topics

Was this topic helpful?