Hello, and welcome to another episode of the Software Carpentry lecture on MATLAB programming. In this episode, we'll have a look at file input and output.
Scientists use a lot of data, and it takes many forms. Almost all of the data that we use can be interpreted as an array, and MATLAB has the tools to convert many common data formats into array structures.
In this lecture, we investigate MATLAB's data input and output mechanisms. We cover three topics. First, we look at a few functions that open files and translate their file format to a MATLAB data structure. We also investigate functions to print formatted output to the screen. Last, we investigate functions to write arrays and other data structures in common file formats.
Importing data can be done in two ways. The easiest is to use the graphical interface directly.
The graphical interface is easiest for quickly loading a single file, but it isn't really feasible if you have to load a lot of files.
The alternative is to call import functions directly.
Command line functions can be automated in scripts and functions. Usually, MATLAB can figure out what to do with a file based on its format, but if it is unclear how to interpret your file, you can provide hints to the import functions when you use the command line.
The graphical interface has a box that displays the contents of the working directory.
One of the files in this folder is named satellite.mat.
Files with the "dot mat" extension are MATLAB storage files that store data in a binary format. Since they are designed for use in MATLAB, MAT files can store multiple variables, which makes it easy to save an entire work session in one file.
Double clicking on the satellite.mat imports all of the variables into the workspace. In this case, there was only one variable called HRImage. When variables are stored in a .mat file, they keep their original names. Be careful about opening multiple files that contain variables with the same names, as this can overwrite old data.
Here is an example of a common file format. Each row of this file contains three numbers separated by a space. Text files are not the most efficient way to store data, but they are nice because humans can quickly check what is in the file.
There are several commands that can handle data from text files, but the most flexible is the command importdata.
Importdata is a wrapper function, which means its job is to figure out what format the file is using and identify the correct function to read the data. Sometimes, importdata may have trouble identifying the correct format of a file. This is especially true if the file uses an unexpected column delimiter or has multiple header lines. Type "help importdata" to see more information about the optional arguments.
Wrapper functions help hide internal details from the end user. In this case, importdata will call a function like load, xmlread, or imread depending on the file. Users do not have to remember which function to use in every situation.
Importdata returns the input as a structure. If you haven't done so already, it would be a good idea to look at the Software Carpentry episode on MATLAB data structures, including the episode on cell arrays.
When we inspect the elements of the structure, we see that the file's information was broken into a large, 3 column matrix called data…
…a cell array of strings called colheaders that are the column headers for each column in our matrix…
…and a third item called textdata that contains a copy of the column headers. If we had more lines of text at the start of our file, these would have been stored in a nested cell array in textdata.
The structure's format depends on the type of file that is imported. For instance, we can read this spreadsheet using the same importdata function.
The result is a structure, and each element of the structure is a sheet in the spreadsheet.
The other half of file IO is retrieving results from MATLAB. There are multiple ways to save data. We can save a plot or image of our data, we can save printed output such as a series of commands and their results, or we can export variables to a file.
Visualization is a large topic that deserves its own episode…