Lecture Notes 10: Input and Output

Reading: Ch. 4



Overview

These notes provide a brief summary of commands that related to presenting output or reading input with MATLAB. Since output is typically presented as a sequence of characters, we begin by discussing the representation of and manipulation of character strings in MATLAB. Then we discuss the standard input and output when a user interacts with a MATLAB program. Similar techniques are used for interacting with data in files.


Character Strings

A vector of characters

A character string, such as 'this is a test' is stored as one-dimensional array of characters, with each individual character encoded numerically (e.g., with ASCII). This means that you can use standard vector operations, such as indexing and slicing.

greeting = 'how are you?';
length(greeting)            % returns 12
find(greeting == ' ')       % returns [4 8]
sort(greeting)              % returns '?aehooruwy'
This means that we can also compose longer strings by concatenating others.
firstName = 'Edsger';
lastName = 'Dijkstra';
fullName = [ firstName ' ' lastName];   % notice the explicit space being concatenated

ASCII values

Each character is really stored as a binary number based on a fixed encoding scheme. The standard encoding for MATLAB is known as ASCII (Unicode is another common encoding scheme that is an extension ASCII). You can find a complete table of all ASCII codes online, such as at www.asciitable.com.

It is possible to compute the underlying numbers associated with characters, as in the following

uint8('A')                    % returns 65 (ASCII code for 'A')
uint8('a')                    % returns 97 (ASCII code for 'a')
unit8('hello')                % returns [104 101 108 108 111]
or to compute the underlying characters, associated with numbers
char(97)                      % returns 'a'
char([83 76 85])       % returns 'SLU'
The implicit conversion of a string to a numerical vector happens as well if you attempt to perform arithmetic on a string.
greeting = 'hello';
greeting .* 3           % returns [312   303   324   324   333]
A convenient use combining these techniques is to advance to a different place in the alphabet using a syntax such as
char('A' + 3);      % will be 'D'

Numeric conversions

Separate from the issue of the underlying ASCII representation of characters, is the distinction between a number such as 123.45 and the six-character string '123.45' that we use to represent that number.

MATLAB provides functions int2str, num2str, and str2num to do conversions between the numbers and their string representations.

As named, int2str converts from an integer to the corresponding string representation.

s = int2str(123);    % s will be string '123'
This can be very useful in conjunction with string concatenation.
low = 1;
high = 10;
prompt = [ 'Pick a number from ', int2str(low), ' to ', int2str(high) ];

The num2str function is similar, but it can convert floating-point numbers to strings. By default, it uses up to four digits after the decimal point. The maximum number to use can be controlled by sending a second parameter.

num2str(pi);        % seems to return '3.1416'
num2str(pi,7);      % returns '3.141593'

The str2num function converts in the opposite direction. If you send it a character string that corresponds to the representation of a numeric value, it returns the numeric value.

General string formatting with sprintf

A far more general tool for formating strings is the sprintf function. It allows you to specify the number of digits to use when display data, as well as whether it should be left or right justified.

For example, we can use sprintf to create our earlier prompt as

prompt = sprintf('Pick a number from %d to %d.', low, high);
Each %d is a place holder for a number that will be expressed later as arguments (namely low and high in this example).

As an aside, the functionfprintf operates much like sprintf but prints output directly to the command window rather than storing the result in a variable. Hence, the commands

prompt = sprintf('Pick a number from %d to %d.', low, high);
disp(prompt);
and the command
fprintf('Pick a number from %d to %d.\n', low, high);
are equivalent. Note the \n at the end of the second command. This tells MATLAB to print a new line at that point. Failing to include this newline marker will generate poorly formatted output. This is discussed further below.

Commonly used place holders include
%d integer displayed in base 10 (decimal) notation
%f floating-point value displayed as 1234.567
%e floating-point value displayed with exponential notation (e.g., 1.234567e+03)
%g more compact of %e and %f
%s character string

Additional details about the desired format can be specified using a syntax such as the following
%8d pad the number with leading spaces to use at least 8 columns (i.e., right-justified)
%08d pad the number with leading zeros to use at least 8 columns
%-8d pad the number with trailing spaces to use at least 8 columns (i.e., left-justified)
%8f print the number using at least 8 columns (padding with spaces if necessary). Note that the decimal point itself is one of those 8 columns, and this form does not make explicitly how many numbers are displayed after the decimal point.
%08f same as previous, but padding with zeros rather than spaces.
%8.3f print the number using at least 8 columns (padding with spaces if necessary), and precisely 3 digits trailing the decimal point.
%.3f print the number using precisely 3 digits trailing the decimal point. The overall width of the number will depend upon the magnitude that comes before the decimal place.
%8s String printed with leading spaces if necessary so as to use at least 8 columns (i.e., right-justified)
%-8s String printed with trailing spaces if necessary so as to use at least 8 columns (i.e., left-justified)
Please note that for all of the above, the overall width specifier is only a minimum number of columns. MATLAB may use more than that many columns if the value requires as such.

Vectorized operations.
The arguments to a sprintf command can be specified individually or as a vector or array. Also, if more elements are provided then are required by the formatting string, it repeats the string for each consecutive group of specified values. This can be used to produce a formatted string for all entries of a vector or even all columns of an array.

matlab> disp(sprintf('%5d', 2 .^ (1:8)));
    2    4    8   16   32   64  128  256
matlab> temp = [1:8;  2.^(1:8)];
matlab> disp(sprintf('Two to the power of %d is %5d\n', temp));
Two to the power of 1 is     2
Two to the power of 2 is     4
Two to the power of 3 is     8
Two to the power of 4 is    16
Two to the power of 5 is    32
Two to the power of 6 is    64
Two to the power of 7 is   128
Two to the power of 8 is   256

For a more complete description of the formatting syntax, see the official MATLAB documentation. These are used for sprintf as well as fprintf.

General string parsing with sscanf

The sprintf command is used to combine pieces of data into a single formatted string. There are times where we have a single string that we wish to interpret as a sequence of individual data values. For example, we might have the string raw = '2009 01 30 8000.86' that was entered by a user or read from a file, and we expect that it is formatted to describe a year, month, date, and closing price for the stock market. We can parse that string by using the syntax
sscanf(raw, '%d %d %d %f')
which returns an array of length four containing the numeric data. sscanf is typically used to read either all numeric values or all string values. Read the documentation for a discussion of what happens if you read both numbers and strings.

User I/O

In the preceding section, we discussed MATLAB's treatment of character strings and the conversions back and forth between other data types and strings. In this section, we focus more carefully on techniques for interacting with the user of a program.

Controlling Timing

Displaying Output

Here are three ways to display data to the user.

Receiving Input