Main Content

Subclasses of MATLAB Built-In Types

MATLAB Built-In Types

Built-in types represent fundamental kinds of data such as numeric arrays, logical arrays, and character arrays. Other built-in types like cell arrays and structures contain data belonging to any class.

Built-in types define methods that perform operations on objects of these classes. For example, you can perform operations on numeric arrays such as, sorting, arithmetic, and logical operations.

See Fundamental MATLAB Classes for more information on MATLAB® built-in classes.

Note

Defining a class with the same name as a built-in class is not supported.

Built-In Types You Can Subclass

You can subclass MATLAB numeric classes and the logical class. For a list of numeric types, see Numeric Types.

You cannot subclass any class that has its Sealed attribute set to true. To determine if the class is Sealed, query the class metadata:

mc = ?ClassName;
mc.Sealed

A value of 0 indicates that the class is not Sealed and can be subclasses.

Why Subclass Built-In Types

Subclass a built-in class to extend the operations that you can perform on a particular class of data. For example, when you want to:

  • Perform unique operations on class data

  • Use methods of the built-in class and other built-in functions directly with objects of the subclass. For example, you do not need to reimplement all the mathematical operators if you derived from a class such as double that defines these operators.

Which Functions Work with Subclasses of Built-In Types

Consider a class that defines enumerations. It can derive from an integer class and inherit methods that enable you to compare and sort values. For example, integer classes like int32 support all the relational methods (eq, ge, gt, le, lt, ne).

To see a list of functions that the subclass has inherited as methods, use the methods function:

methods('SubclassName')

Generally, you can use an object of the subclass with any:

  • Inherited methods

  • Functions that normally accept input arguments of the same class as the superclass.

Behavior of Built-In Functions with Subclass Objects

When you define a subclass of a built-in class, the subclass inherits all the methods defined by that built-in class. MATLAB also provides additional methods to subclasses of built-in classes that override several built-in functions.

Built-in functions and methods that work on built-in classes can behave differently when called with subclasses of built-in classes. Their behavior depends on which function you are using and whether your subclass defines properties.

Behavior Categories

When you call an inherited method on a subclass of a built-in class, the result depends on the nature of the operation performed by the method. The behaviors of these methods fit into several categories.

  • Operations on data values return objects of the superclass. For example, if you subclass double and perform addition on two subclass objects, MATLAB adds the numeric values and returns a value of class double.

  • Operations on the orientation or structure of the data return objects of the subclass. Methods that perform these kinds of operations include, reshape, permute, transpose, and so on.

  • Converting a subclass object to a built-in class returns an object of the specified class. Functions such as uint32, double, char work with subclass objects the same as they work with built-in objects.

  • Comparing objects or testing for inclusion in a specific set returns logical or built-in objects, depending on the function. Functions such as isequal, ischar, isobject work with subclass objects the same as they work with superclass objects.

  • Indexing expressions return objects of the subclass. If the subclass defines properties, then default indexing no longer works. The subclass must define its own indexing methods.

  • Concatenation returns an object of the subclass. If the subclass defines properties, then default concatenation no longer works and the subclass must define its own concatenation methods.

To list the built-in functions that work with a subclass of a built-in class, use the methods function.

Built-In Subclasses That Define Properties

When a subclass of a built-in class defines properties, MATLAB no longer supports indexing and concatenation operations. MATLAB cannot use the built-in functions normally called for these operations because subclass properties can contain any data.

The subclass must define what indexing and concatenation mean for a class with properties. If your subclass needs indexing and concatenation functionality, then the subclass must implement the appropriate methods.

Methods for Indexing

To support indexing operations, the subclass must implement these methods:

  • subsasgn — Implement dot notation and indexed assignments

  • subsref — Implement dot notation and indexed references

  • subsindex — Implement object as index value

Note

Modular indexing mixin classes were introduced in R2021b, but these mixins are not compatible with subclasses of built-in classes. See Code Patterns for subsref and subsasgn Methods for more information on how to implement subsref, subsasgn, and subsindex.

Methods for Concatenation

To support concatenation, the subclass must implement the following methods:

  • horzcat — Implement horizontal concatenation of objects

  • vertcat — Implement vertical concatenation of objects

  • cat — Implement concatenation of object arrays along specified dimension

Related Topics