Subclasses of MATLAB Built-In Types - MATLAB & Simulink (original) (raw)

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:

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:

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

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.

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:

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, andsubsindex.

Methods for Concatenation

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

See Also

Topics