Find us on GitHub

Teaching basic lab skills
for research computing

MATLAB: More I/O

MATLAB/More I/O at YouTube

Hello, and welcome to another episode of the Software Carpentry lecture on MATLAB programming. In this episode, we will talk about printing formatted output to the screen and exporting data to a file.

Before we starting printing to the screen, it is important to figure out what kind of output is appropriate to print to the screen. One common practice is to print intermediate status messages in functions that run for a long time. A good status message can help the user abort a program if it is not performing correctly.

Functions that iterate an unknown number of times often have status messages. An example of such a function is the power iteration algorithm, which finds the largest eigenpair of a matrix. It is often used on large, sparse matrices such as the matrix of weights used by Google's PageRank algorithm. The basic algorithm is embodied in the red equation, which is just a matrix multiplication and division by a scalar

Here is a piece of MATLAB code that computes the power iteration of a matrix A. Note that we loop over the iteration until successive approximations b and b_new differ by at most 1e-5.

If we define a large matrix and compute the power iteration, there is no guarantee on how long the algorithm will run. There are several things that can go wrong, including a coding error or floating point error that leads to an infinite loop.

One way to monitor the calculation is to compute the stopping criteria and leave off the semicolon.

This is not a good idea, because it prints 4 lines per iteration. If we need to print several status variables per iteration, the output is both long and hard to read.

A better strategy is to utilize the fprintf function, which prints formatted text to a file or to the standard output stream.

Clearly, this output is cleaner and shorter than it would be if we had just left off a semi-colon.

fprintf is mirrored on the printf statement from C or FORTRAN. The important input is format, which is a string that contains special escape sequences that start with a percent sign. Each percent sign corresponds to another parameter that will be printed at the specified location.

The numbers and digits that follow a percent are hints on how to format the number. For instance, %d means to interpret the corresponding value as an integer, and we see that the input i makes sense as an integer.

Percent f means to interpret the value as a floating point number, so the norm is printed in floating point format. There is a long list of formatting symbols that is available at the online help for fprintf.

Another important use of printed output is for auditing during data exploration. If you are exploring several kinds of analysis, journaling with the diary function can help keep track of what commands were run and what their output was.

MATLAB always tracks the history of commands that were run from the standard prompt. These can be access from the history screen or by typing the up key in the prompt.

History gets us halfway there. It stores what commands we ran, but it does not store a transcript of the actual output. Using diary, we can find changes to output that might be caused by changes to functions or data files.

To turn on the diary, type diary and a filename. If you don't specify a file name, the output is stored in a file called diary. You can turn the diary on and off by typing diary on and diary off.

Unfortunately, there is no single function that can store any kind of data. This should make sense, because it is important to specify how you want to interpret an array or structure.

If you are saving data that will be reloaded by MATLAB, it is probably best to use MAT files that can store your entire workspace. However, keep in mind that MAT files can not be examined in a text editor, and you are reliant on MATLAB or a third party program to read the files later on. Another option for some data is XML, which is a formatted text file that can store multiple variables.

Many times, it is important to export data as a sound or image or other medium. MATLAB provides functions to translate arrays or structures into many media formats. Some special video or image formats might require codecs to correctly encode a file, and these can be found at MATLAB's file central.

The important point is that most data can be interpreted as an array. MATLAB can import data in many different formats, and it provides tools to export data structures back into many media and storage formats. In the next episode, we will look at visualization in MATLAB, which includes tools to create plots and images.