Up: MATLAB

Basic I/O

August 10th, 2011 Leave a comment Go to comments

slide 001 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.
slide 002 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.
slide 003 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.
slide 004 Importing data can be done in two ways. The easiest is to use the graphical interface directly.
slide 005 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.
slide 006 The alternative is to call import functions directly.
slide 007 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.
slide 008 The graphical interface has a box that displays the contents of the working directory.
slide 009 One of the files in this folder is named satellite.mat.
slide 010 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.
slide 011 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.
slide 012 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.
slide 013 There are several commands that can handle data from text files, but the most flexible is the command importdata.
slide 014 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.
slide 015 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.
slide 016 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.
slide 017 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…
slide 018 …a cell array of strings called colheaders that are the column headers for each column in our matrix…
slide 019 …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.
slide 020 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.
slide 021 The result is a structure, and each element of the structure is a sheet in the spreadsheet.
slide 022 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.
slide 023 Visualization is a large topic that deserves its own episode…
slide 024

  1. No comments yet.
  1. No trackbacks yet.