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. Hence, the need to prompt users to input data is not as prevalent in MATLAB as in most other programming languages. But, of course, there is a function for when it is needed.

The function result = input(’PROMPT’) displays the prompt on the screen and returns the number entered by the user. The returned 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 also 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 is desired, pass the ’s’ option 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 it when you enter a variable name in the Command Window without the semicolon. The fprintf function allows you to customize how the output is formatted.

2.4.2.1. disp

The disp function takes only one argument and generally formats the output acceptably. There are two options for displaying multiple outputs, such as a text string and a number. 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, the num2str function converts 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

2.4.2.2. 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. 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. The format specifier often contains additional special characters called escape characters. A new line is 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. Programming beginners may need practice using fprintf. The examples below can serve as a guide for how to use fprintf. MATLAB’s documentation for fprintf offers more details.

The data type codes in the format specifier are listed in fprintf format specifiers followed by some examples. Data type codes ’s’, ’d’, ’g’, ’f’, and ’e’ are the most frequently used codes.

Table 2.4 fprintf format specifiers

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
>> 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.