Accelerating the pace of engineering and science

Documentation Center

• Trial Software

disp

Display text or array

Description

example

disp(X) displays the contents of X without printing the variable name. disp does not display empty variables.

Examples

expand all

Display Matrix with Column Labels

Display a matrix and label the columns as Corn, Oats, and Hay.

```X = gallery('uniformdata',[5 3],0);
disp('     Corn      Oats      Hay')
disp(X)```
```     Corn      Oats      Hay
0.9501    0.7621    0.6154
0.2311    0.4565    0.7919
0.6068    0.0185    0.9218
0.4860    0.8214    0.7382
0.8913    0.4447    0.1763```

Include the full hypertext string on a single line as input to disp.

```X = '<a href = "http://www.mathworks.com">MathWorks Web Site</a>';
disp(X)```
`MathWorks Web Site`

The disp function generates a hyperlink in the Command Window. Click the link to display the MathWorks home page in a MATLAB® Web browser.

Display Multiple Items on Same Line

Concatenate strings together using the [] operator. Convert any numeric values to characters using the num2str function.

```name = 'Alice';   age = 12;
X = [name, ' will be ', num2str(age), ' this year.'];
```

Display the string.

`disp(X)`
`Alice will be 12 this year.`

You also can use sprintf to create a string. Terminate the sprintf command with a semicolon to prevent "X = " from being displayed. Then, use disp to display the string.

```name = 'Alice';   age = 12;
X = sprintf('%s will be %d this year.', name, age);
disp(X)```
`Alice will be 12 this year.`

Alternatively, use fprintf to create and display the string. Unlike the sprintf function, fprintf does not display the "X = " text. However, you need to end the string with the newline (\n) metacharacter to terminate its display properly.

```name = 'Alice';   age = 12;
X = fprintf('%s will be %d this year.\n', name, age);```
`Alice will be 12 this year.`

Input Arguments

expand all

X — Variable to displayvariable name

Variable to display, specified by the variable name.

expand all

Tips

• The disp function accepts only one input. To display more than one array or string, you can use concatenation or the sprintf or fprintf functions as shown in the Example, Display Multiple Items on Same Line.