disp
Display value of variable
Syntax
Description
Examples
Display Variable Values
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 Matrix with Column Labels
Display a matrix and label the columns as Corn
, Oats
, and Hay
.
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 Hyperlink in Command Window
Display a link to a Web page by including HTML hyperlink
code as input to disp
. For example, display
a link to the MathWorks® Web site.
X = '<a href = "https://www.mathworks.com">MathWorks Web Site</a>';
disp(X)
Display Multiple Variables on Same Line
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 the num2str
function. Use disp
to display the result.
name = 'Alice'; age = 12; X = [name,' will be ',num2str(age),' this year.']; disp(X)
Alice will be 12 this year.
Use sprintf
to create text, and then display it with disp
.
name = 'Alice'; age = 12; X = sprintf('%s will be %d this year.',name,age); disp(X)
Alice will be 12 this year.
Use fprintf
to 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
X
— Input array
array
Input 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.
Extended Capabilities
Thread-Based Environment
Run code in the background using MATLAB® backgroundPool
or accelerate code with Parallel Computing Toolbox™ ThreadPool
.
This function fully supports thread-based environments. For more information, see Run MATLAB Functions in Thread-Based Environment.
Version History
Introduced before R2006a
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list:
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)