| Contents | Index |
disp(X)
disp(X) displays an array, without printing the array name. If X contains a text string, the string is displayed.
Another way to display an array on the screen is to type its name, but this prints a leading "X=," which is not always desirable.
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 Example 3 — Display multiple items on the same line.
Note that disp does not display empty arrays.
This example uses disp to display a matrix with column labels:
disp(' Corn Oats Hay')
x = gallery('uniformdata',[5 3],0);
disp(x)This results in
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.1763You also can use the disp command to display a hyperlink in the Command Window. Include the full hypertext string on a single line as input to disp:
disp('<a href = "http://www.mathworks.com">MathWorks Web Site</a>')which generates this hyperlink in the Command Window:
MathWorks Web Site
Click the link to display the MathWorks home page in a MATLAB Web browser.
Use any of the following techniques to display multiple items on the same line:
Concatenate all substrings together using the [] operator. Convert any numeric values to characters using the num2str function:
name = 'Alice'; age = 12; str = [name, ' will be ', num2str(age), ' this year.'] Alice will be 12 this year.
Use sprintf to create the string, and disp to display it. Terminate the sprintf command with a semicolon. This keeps "str = " from being displayed:
name = 'Alice'; age = 12;
str = sprintf('%s will be %d this year.', name, age);
disp(str);
Alice will be 12 this year.Use fprintf to create and display the string. Unlike the sprintf function, fprintf does not display the "str = " text. However, you do need to end the string with the newline (\n) metacharacter to terminate its display properly:
name = 'Alice'; age = 12;
str = fprintf('%s will be %d this year.\n', name, age);
Alice will be 12 this year.colon (:) | format | fprintf | int2str | num2str | rats | sprintf
| © 1984-2012- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |