Documentation

This is machine translation

Translated by Microsoft
Mouse over text to see original. Click the button below to return to the English verison of the page.

Overloading 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 matlab.mixin.CustomDisplay, overload the disp function to change how MATLAB® displays objects of your class.

MATLAB calls the display function 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 =
    5

display then calls disp.

Overloaded disp

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. MATLAB calls display with two arguments and passes the variable name as the second argument.

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.

  • There is no left-side variable, then MATLAB prints ans = followed by the value.

  • Code explicitly invokes the display function.

When invoked display:

  • If the input argument is an existing variable, display prints the variable name and equal sign, followed by the value.

  • If the input is the result of an expression, display does not print ans =.

MATLAB invokes the built-in disp function when:

  • The built-in display function calls disp.

  • Code explicitly invokes disp.

For empty built-in types (numeric types, char, struct, and cell) the display function displays:

  • [] — for numeric types

  • "0x0 struct array with no fields." — for empty structs.

  • "Empty cell array: 0-by-1" — for empty cell arrays.

  • '' — for empty char arrays

disp differs from display in these ways:

  • disp does not print the variable name or ans.

  • disp prints nothing for built-in types (numeric types, char, struct, and cell) when the value is empty.

Related Examples

Was this topic helpful?