disp

Display text or array

Syntax

Description

example

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

Examples

expand all

Display Variable Contents

Create a numeric array, A, and a string, S.

A = [15 150];
S = 'Hello World.';

Display each variable.

disp(A)
    15   150
disp(S)
Hello World.

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

Display Hyperlink in Command Window

Display a hyperlink to a Web page.

Include the HTML hyperlink code 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 Variables on Same Line

There are three ways to display multiple variables on the same line in the Command Window.

Concatenate multiple strings together using the [] operator. Convert any numeric values to characters using the num2str function. Then, use disp to display the string.

name = 'Alice';   
age = 12;
X = [name,' will be ',num2str(age),' this year.'];
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 — Data to displayany type of array

Data to display, specified as any type of array.

To display more than one array, you can use concatenation or the sprintf or fprintf functions as shown in the example, Display Multiple Variables on Same Line.

Was this topic helpful?