Main Content

matlab.mixin.Scalar Class

Namespace: matlab.mixin
Superclasses: matlab.mixin.indexing.RedefinesParen

Enforce scalar behavior for class instances

Since R2021b


Instances of classes that inherit from matlab.mixin.Scalar always behave as scalars:

  • The array size is always 1-by-1.

  • The creation of empty arrays is prohibited.

  • Concatenation of instances is prohibited.

Scalar inherits from matlab.mixin.indexing.RedefinesParen. The protected methods that enable parentheses indexing—parenReference, parenAssign, parenDelete, and parenListLength—error by default in Scalar. You can overload these methods to customize parentheses behavior for your class.

An example of a use case for this mixin is a class that acts as an interface to a piece of hardware, such as a robotic device. Forming an array of instances of such a class might not make sense because each device can be in a different state or supporting different operations. Concatenating several instances of device states also does not have an obvious use or interpretation.

Class Attributes


For information on class attributes, see Class Attributes.


expand all


collapse all

This example shows how to customize dot indexing operations in the ScalarStructClass. Instances of ScalarStructClass behave much like structs. Users can dynamically add fields and associated values like a struct, but the class also serves as a base to which additional properties and methods can be added.

The class inherits from matlab.mixin.Scalar, which means its instances are scalar objects. The only possible array size is 1-by-1, and the instances cannot be concatenated. The class also inherits from matlab.mixin.indexing.RedefinesDot and implements its abstract methods to handle dot indexing operations:

  • dotReference: Handles dot references into the private AddedFields property. The syntax instance.fieldname returns the value assigned to the referenced field.

  • dotAssign: Adds a new field and corresponding value to the AddedFields property. The syntax instance.fieldname = value adds the field and its corresponding value.

  • dotListLength: Determines the number of values to return from dot indexing expressions that return values from or assign values to a comma-separated list.

The class also defines the getAddedFields method, which returns a list of all fields and corresponding values.

ScalarStructClass Code

classdef ScalarStructClass < matlab.mixin.indexing.RedefinesDot & ...
    properties (Dependent, SetAccess=private)

    properties (Access=private)
        AddedFields struct

        function out = get.FieldNames(obj)
            out = string(fieldnames(obj.AddedFields));

    methods (Access=public)
        function obj = ScalarStructClass(fieldName,fieldValue)
            if nargin == 1
                obj.AddedFields = fieldName;
            obj.AddedFields = struct(fieldName,fieldValue);

        function out = getAddedFields(obj)
            out = obj.AddedFields;

    methods (Access=protected)
        function varargout = dotReference(obj,indexOp)
            [varargout{1:nargout}] = obj.AddedFields.(indexOp);

        function obj = dotAssign(obj,indexOp,varargin)
            [obj.AddedFields.(indexOp)] = varargin{:};
        function n = dotListLength(obj,indexOp,indexContext)
            n = listLength(obj.AddedFields,indexOp,indexContext);

Use a ScalarStructClass Instance

Construct a ScalarStructClass instance with one field and a corresponding value.

myStructClass = ScalarStructClass("Field1",75)
myStructClass = 
  ScalarStructClass with properties:

    FieldNames: "Field1"

Add a second field to the instance using dot assignment. The dotAssign method accepts an IndexingOperation object, which describes the type of indexing operation (Dot) and the name of the field, and a second argument that contains the value of the new field.

myStructClass.Field2 = 10;

Use dot notation to verify the value of Field2. Like dotAssign, the dotReference method accepts an IndexOperation object that identifies what field to access.

ans = 10

Use getAddedFields to see the full list of fields and values.

ans = struct with fields:
    Field1: 75
    Field2: 10

ScalarStructClass also supports comma-separated list assignment and reference. Add a third field to myStructClass that contains a cell array.

myStructClass.CellArray = {3 4};

Access the CellArray field. The class calls the dotReference method and returns multiple outputs.

[v1,v2] = myStructClass.CellArray{:}
v1 = 3
v2 = 4

Assign multiple new values to the CellArray field. Because the assignment operation begins with a dot reference and ends with a brace index, the class calls the dotListLength method in addition to dotAssign to handle the assignment operation.

[myStructClass.CellArray{:}] = deal(5,6);
ans=1×2 cell array
    {[5]}    {[6]}

Version History

Introduced in R2021b