disp

Display value of variable

Syntax

Description

example

disp(X)displays the value of variableXwithout printing the variable name. Another way to display a variable is to type its name, which displays a leading “X =” before the value.

If a variable contains an empty array,dispreturns without displaying anything.

Examples

collapse all

Create a variable with numbers and another variable with text.

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

Display the value of each variable.

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

Display a matrix and label the columns asCorn,Oats, andHay.

X = rand(5,3); disp(' Corn Oats Hay')
Corn Oats Hay
disp(X)
0.8147 0.0975 0.1576 0.9058 0.2785 0.9706 0.1270 0.5469 0.9572 0.9134 0.9575 0.4854 0.6324 0.9649 0.8003

Display a link to a Web page by including HTML hyperlink code as input todisp. For example, display a link to the MathWorks®Web site.

X ='MathWorks Web Site'; disp(X)

Here are three ways to display multiple variable values on the same line in the Command Window.

Concatenate multiple character vectors together using the[]operator. Convert any numeric values to characters using thenum2strfunction. Usedispto display the result.

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

Usesprintfto create text, and then display it withdisp.

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

Usefprintfto directly display the text without creating a variable. However, to terminate the display properly, you must end the text with the newline (\n) metacharacter.

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

Input Arguments

collapse all

Input array.

To display more than one array, you can use concatenation or thesprintforfprintffunctions as shown in the example,显示多个变量Same Line.

Introduced before R2006a