display

Show information about variable or expression result

Description

example

display(X) is called by MATLAB® when a statement or expression is not terminated by a semicolon. Omit the terminating semicolon from a statement or expression when you want to see an intermediate result.

MATLAB calls the display function to show information about an intermediate result, such as the values, size, type, and variable name.

To show the value of a variable or to show program output in the command window, use the disp function.

To customize the display of user-defined objects, use the techniques described in the Customize Object Display for Classes topic.

Examples

collapse all

MATLAB calls display when you make an assignment to a variable without terminating the statement with a semicolon. In this example, display shows the variable name and the value.

a = 7
a =

     7

MATLAB does not call display when you make an assignment to a variable and the statement is terminated with a semicolon.

a = 7;

When you execute an expression without a semicolon, MATLAB assigns the result to a variable called ans, which the display function shows in the command window.

format long
sqrt(2)
ans =

   1.414213562373095

Input Arguments

collapse all

Result of executing a statement or expression, passed to the display function by MATLAB.

More About

collapse all

Assignment to ans

Executing an expression without terminating the expression with a semicolon causes the result to be displayed in the command window. MATLAB assigns the result of an expression to a variable named ans when the result is not assigned to a variable explicitly.

4 * 5 - 13
ans =

    7

To display the result in the command window without displaying ans, use the disp function.

disp(4 * 5 - 13)
7

If an expression is terminated by a semicolon, MATLAB does not display a value, but still assigns the result to the ans variable.

4 * 5 - 13;
ans
ans =

     7

Display Results in Command Window

Omitting the terminating semicolon is useful when you want to see intermediate results from statements in a program. For example, compare these two statements by omitting the semicolon. The display function shows the results in the command window.

result1 = 4 * 5 - 13
result1 =

     7
result2 = 4 * (5 - 13)
result2 =

   -32

Information Shown by the display Function

The display function provides information about the kind of values that are the result of executing a statement or expression. This information is useful for understanding how a program or script works.

For example, this statement assigns a uint8 vector of values 1 2 3 4 to the variable named a. The display function shows the variable name, the size and type, and the values.

a = uint8([1 2 3 4])
a =

  1×4 uint8 row vector

   1   2   3   4

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

  • [] — for numeric types

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

  • "0x0 empty cell array" — for empty cell arrays.

  • "0x0 empty char array" — for empty char arrays

  • "0x0 empty string array" — for empty string arrays

To show the actual values that are the intended output from a program, like text and numbers, call the disp function. The disp function does not display the variable name or ans. Also, disp displays nothing for built-in types (numeric types, char, struct, and cell) when the value is empty.

Introduced before R2006a