Overload the disp Function

Display Methods

Subclassing matlab.mixin.CustomDisplay is the best approach to customizing object display. However, if you do not derive your class from CustomDisplay, you can overload the disp function to change how MATLAB® displays objects of your class.

MATLAB calls an object's display method whenever an object is referred to in a statement that is not terminated by a semicolon. For example, the following statement creates the variable a. MATLAB calls display, which displays the value of a in the command line.

a = 5
a =

display then calls disp.

Implement disp or disp and display

The built-in display function prints the name of the variable that is being displayed, if an assignment is made, or otherwise uses ans as the variable name. Then display calls disp to handle the actual display of the values.

If the variable that is being displayed is an object of a class that overloads disp, then MATLAB always calls the overloaded method. Overload disp or disp and display to customize the display of objects. Overloading only display is not sufficient to properly implement a custom display for your class.

Relationship Between disp and display

MATLAB invokes the built-in display function when:

  • MATLAB executes a statement that returns a value and is not terminated with a semicolon.

  • Code explicitly invokes the display function.

MATLAB invokes the built-in disp function when:

  • The built-in display function calls disp.

  • Code explicitly invokes disp.

Was this topic helpful?