Documentation

Subclassing 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 contain data belonging to these fundamental types and other classes. For example, cell and struct arrays can contain instances of 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, rounding values, and element-wise and matrix multiplication. You can create an object of class double using an assignment statement, indexing expressions, or using converter functions.

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

    Note:   It is an error to define a class that has the same name as a built-in class.

Built-In Types You Cannot Subclass

You cannot subclass the following built-in MATLAB classes:

  • char

  • cell

  • struct

  • table

  • function_handle

In general, you cannot subclass any class that has its Sealed attribute set to true. Query the class meta-data to determine if the class is Sealed:

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 type to extend the operations that you can perform on a particular class of data. For example, when you want to:

  • Define unique operations to perform on class data.

  • Be able to 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 built-in class methods. In addition, MATLAB provide a number of built-in functions as subclass methods.

However, built-in functions that work on built-in classes behave differently with subclasses. 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 of that call 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 provides support for 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

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

Was this topic helpful?