Program Output
Matlab provides different ways to control when and how information is displayed or output to the user. You can enter a command in Matlab and the result of that command is displayed in the command window unless a semicolon ( ; ) is used to suppress the output.Another more powerful way to display information is with the
disp()
function. The disp()
command accepts a
single input value and displays the contents of that input value to the
user. You can use the disp()
function to:
- display a text string
-
>> disp( 'Hello World' ); Hello World
- display just the contents of a variable
>> x = 2.7e-14; >> disp( x ); 2.7000e-014
- display a phrase that includes text and values
>> disp( [ 'The value of x is: ' , num2str(x) ] ); The value of x is: 2.7e-014
- display a phrase that includes two text strings and values
>> disp( [ 'The value of x is: ' num2str(x), ... ' and the value of y is ' num2str(y) '.' ] ); The value of x is: 2.7e-014 and the value of y is 3.78e-12.
Note: To display text strings and values, convert the values to
strings using num2str()
and wrap all in a matrix, as shown.