2.4. Input and Output¶
2.4.1. Input Function¶
Scripts share the global Workspace variables with the Command Window, and functions receive input data from passed arguments, so the need to prompt users to input data is not prevalent in MATLAB as is the case for most other programming languages. But, of course, there is a function for cases when it is needed.
The function result = input('PROMPT')
displays the prompt on the
screen and returns the number entered by the user. The return data is of
type double as expected, but may be cast to another data type.
>> num = input('Enter a number: ')
Enter a number: 15
num =
15
>> intNum = int32(input('Enter an integer number:' ))
Enter an integer number:8
intNum =
int32
8
The input()
function evaluates what the user enters, so more complex
data such as a vector and matrix can even be entered.
>> vect = input('Enter a vector: ')
Enter a vector: [1 2 3]
vect =
1 2 3
>> vect = input('Enter a vector: ')
Enter a vector: 1:4
vect =
1 2 3 4
If a string result is desired, pass the option 's'
to input()
.
>> name = input('What is your name? ', 's' )
What is your name? Tim
name =
'Tim'
2.4.2. Output Functions¶
Two MATLAB functions are commonly used to display results in the Command
Window. The disp()
function is generally easier to use because it
formats the output in the same way as you see when you enter a command in
the Command Window without the semicolon. The fprintf()
function allows
you to customize how the output is formatted.
-
disp
The disp()
function takes only one argument and generally formats the
output in an acceptable manner. If you want to display multiple outputs,
such as a text string and a number, there are two options. You can use
separate calls to disp()
, or pass one item to disp()
that is
an array (row vector) where each item in the array has the same data type.
In the following example, I use the num2str()
function to convert a
number to a string, thus both items in the array (notice the square
brackets) are strings.
>> disp(['The number is: ', num2str(num)])
The number is: 15
>> disp('Here is the 2x2 identity matrix')
Here is the 2x2 identity matrix
>> disp(eye(2))
1 0
0 1
-
fprintf
The fprintf()
function doesn’t provide as much help in formatting the
output, but offers the programmer complete flexibility to customize the
appearance of the output. When the output is to be displayed in the Command
Window, the first argument to fprintf()
is a string called a format
specifier. The string may contain text to display and also information about
the data type and location for variables to be displayed in the output.
Three additional special characters (called escape characters) are often
used in the format specifier. A new line achieved with \n
. A tab
character is inserted in the output with \t
. To display an actual
backslash, use two backslashes \\
.
Any number of variables may be passed to fprintf()
, but they should
match the variable references in the format specifier. Each variable
reference begins with a percent sign (%
) and uses a letter code to indicate
the data type. In addition, the variable reference can also specify
information such as the number of characters to use when displaying the
variables (called the field width) and the justification (left or right)
within the character field. I’ll leave you to read MATLAB’s documentation
for those details if you find that you need that level of detail.
The data type codes in the format specifier are listed below followed by some examples. Data type codes ‘s’, ‘d’, ‘g’, ‘f’, and ‘e’ are the most frequent used codes.
Letter Meaning s string c character d integer u unsigned integer f floating point g floating point, but fewer digits e scientific notation o octal x hexadecimal
>> fprintf('Number is %g. Integer number is %d.\n', num, intNum)
Number is 15. Integer number is 8.
>> fprintf('Number is %f. Integer number is %d.\n', num, intNum)
Number is 15.000000. Integer number is 8.
>> fprintf('Pi is %f.\n', pi)
Pi is 3.141593.
>> fprintf('Pi is %g.\n', pi)
Pi is 3.14159.
>> fprintf('Pi is %e.\n', pi)
Pi is 3.141593e+00.
>> big = 32.8997898e20
big =
3.2900e+21
>> fprintf('A big number is %e.\n', big)
A big number is 3.289979e+21.
>> fprintf('A big number is %g.\n', big)
A big number is 3.28998e+21.
>> fprintf('A big number is %f.\n', big)
A big number is 3289978979999999852544.000000.