Introduction to Class Customization
User-defined classes share many of the same behaviors as classes that are included with MATLAB®. These behaviors include display, indexing, and even mathematical operations. You can customize these behaviors. For example, you can change the default display of your classes so that properties are grouped under custom headings.
To customize a behavior for your class, you can overload operators, inherit from a mixin class, or overload functions.
Operator Overloading
You can define class methods to overload MATLAB operators. For example, you can overload subtraction so that your classes have specific behaviors for statements like
a – b. In this case, define minus as a method for your class. See Operator Overloading for more information.
Inherit from Mixins
MATLAB provides many mixins that your classes can inherit from to customize specific behaviors. For example:
Custom Display Format – The
matlab.mixin.CustomDisplayandmatlab.mixin.CustomCompactDisplayProvidermixins enable you to control the default display of your classes both at the command line and in compact formats, like table cells. Customize different displays for scalar instances and arrays, and control aspects like how many properties are displayed and how they are grouped. For more information, see Custom Display Interface and Custom Compact Display Interface.Object Serialization – The
matlab.mixin.CustomElementSerializationmixin enables you to customize what information is saved when an object is serialized and how that information is restored during deserialization. For more information, see When to Customize the Serialization Process.Object Indexing – The classes in the
matlab.mixin.indexingnamespace enable you to customize the behavior of indexing operations using parentheses, dots, and brackets. For more general information, see Customize Object Indexing.Copy Behavior – The
matlab.mixin.Copyablemixin implements a copy method for handle classes, enabling you to customize copy behaviors, such as excluding some properties from copying as well as specifying whether to make shallow and deep copies. For more information, see Implement Copy for Handle Classes.
Overload Functions
For functionality that does not have an associated mixin, you can overload
MATLAB functions. For an example of a class that overloads the
bar function, see Overload Functions in Class Definitions.