Importing Data (Plotting in Two Dimensions) (MATLAB)

Whenever we are dealing with data in MATLAB, whether it is function generated, or measured, we are first faced with just how to bring that data into the MATLAB environment. Fortunately MATLAB provides a rich set of commands that support data input and output from many different standard formats. If you have a data file that was created using another application or program, the contents of that data file can be imported into the MATLAB workspace. Once you have imported the data, you can then manipulate or plot the data. However, before we consider data files from other applications, we should also understand how to import data saved during other MATLAB sessions. In many cases, you will be working with other MATLAB users and you will need to operate on their data.

MATLAB Data Formats

Modern MATLAB supports a broad range of standard data formats. The following tables list the data formats for which MATLAB provides built-in support and the associated import commands.


Data Formats

Command

Returns

MAT – MATLAB workspace

LOAD

Variables in file

CSV – Comma separated numbers

CSVREAD

Double array

TXT – Formatted data in a text file


TEXTREAD

Double array

DAT – Formatted text

IMPORTDATA

Double array

DLM – Delimited text

DLMREAD

Double array

TAB – Tab separated text

DLMREAD

Double array

Spreadsheet Formats

Command

Returns

XLS – Excel worksheet

XLSREAD

Double array and cell array

WK1 – Lotus 123 worksheet

WK1READ

Double array and cell array

Scientific Data Formats

Command

Returns

CDF – Common Data Format

CDFREAD

Cell array of CDF records

FITS – Flexible Image Transport System

FITSREAD

Primary or extension table data

HDF – Hierarchical Data Format

HDFREAD

HDF or HDF-EOS data set

Image Formats

Command

Returns

TIFF – Tagged image format

IMREAD

Truecolor, grayscale or indexed image(s)

PNG – Portable network graphics

IMREAD

Truecolor, grayscale or indexed image

HDF – Hierarchial data format

IMREAD

Truecolor or indexed image(s)

BMP – Windows bitmap

IMREAD

Truecolor or indexed image

Audio Formats

Command

Returns

AU – Next/Sun Sound

AUREAD

Sound data and sample rate

SND – Next/Sun Sound

AUREAD

Sound data and sample rate

WAV – Microsoft Wave Sound

WAVREAD

Sound data and sample rate

Movie Formats

Command

Returns

AVI – Movie

AVIREAD

MATLAB movie

Importing High-Level Data

The most straightforward method of importing data is to use the load command. The load command can read either binary files containing matrices generated by earlier MATLAB sessions (usually by use of the save command), or text files containing numeric data. If the data file was created in an earlier MATLAB session, simply issuing the load command with the filename is all that is needed. The save command will save the specified data in MATLAB’s binary data format. The following example shows just how simple this can be.

tmp8414-14_thumb

The important points to remember in using save and load in this way is that MATLAB will by default attach the ".mat" extension to the data file and the file will be created or read from the current working directory.

As stated earlier, this use of the save and load commands uses the default MATLAB binary file format. Although many other applications are now being created that can read and write this format, save and load can be used to both write and read text data which can make importing and exporting data a simple matter. Either command could have been issued with the keyword -ASCII. If save was used with -ASCII, the data is automatically tab delimited. Otherwise, you should make sure that your data file is organized as a rectangular table of numbers, separated by blanks, with one row per line, and an equal number of elements in each row. For example, let’s say that you have an ASCII data file called datafile.dat which contains three columns of data. The first column contains the integers 1 through 10. The second column lists the square root of the first column’s numbers. Finally, the third column contains the square of the numbers in the first column.

tmp8414-15_thumb

The data can then be imported into the MATLAB workspace by typing:

tmp8414-16_thumb

You do not need to specify that the file is an ASCII format as the load command is smart enough to recognize that. MATLAB puts the data contained in the datafile.dat file into a matrix variable called datafile. This matrix will have 10 rows and 3 columns. New variables can be defined from the rows, columns, and elements of the datafile variable. To find out exactly how and what you can do with variables by means of their indices, take a look at the sections in the Getting Started with MATLAB manual.

Importing Low-Level Data

Often data files contain headers, that is, descriptive statements describing how, when, and under what circumstances the following data was collected or generated. Usually you will wish to bypass the header after you have extracted the information you need from it. Additionally, other complicating factors such as rows that have varying number of columns, or text interspersed with numerical data will inevitably be encountered. Even if your data is not in one of the standard formats, you can use the low-level file input/output (I/O) functions MATLAB provides. In such circumstances where the format of the file is known, but is not one of the standard formats, it will most likely be best to make use of the fread and fscanf commands. Both commands are used to read binary data from a file according to a specified format. Both are part of the low-level I/O commands available in MATLAB and require that certain parameters that describe the precision and location of the data in the file be specified. The general form of the fread command is:

tmp8414-17_thumb

Here, A is the matrix returned by the fread command that contains the data which was read. COUNT is an optional output argument that tells you how many elements were successfully read. As you can see, fread expects up to four input arguments. The first argument, FID, is a required value that corresponds to the file identification number of the file to be read. This value is obtained by using the fopen command. The second argument, SIZE, is optional and tells the fread command how much data is to be read. PRECISION is a string that specifies the format of the data. Typically this consists of a data specifier such as int or float followed by an integer giving the size in bits. In general MATLAB’s low-level I/O functions are based on the I/O functions of the ANSI C Library. If you are already familiar with C, then you will be familiar with these commands. The table, "MATLAB Low-Level I/O Commands" lists both the binary and ASCII low-level file I/O commands in MATLAB. The following steps are generally what is required to read and write data to data files:

1.    Open the file to be read or written to using fopen.

2.    Operate on the file:

a.    fread for reading binary data,

b.    fwrite for writing binary data,

c.    fgets or fgetl for reading text strings line by line,

d.    fscanf for reading formatted ASCII    data,

e.    fprintf for writing formatted ASCII    data.

3.    fclose to close the file.

Although the following table can serve as a handy reminder, please refer to the on-line help or to the MATLAB Function Reference to learn more about MATLAB’s low-level file I/O commands.

MATLAB Low-Level I/O Commands

Command

Action

Usage

FOPEN

Opens a file for reading or writing.

FID = FOPEN(‘FILENAME’,'PERMISSION’)

FCLOSE

Used to close a file once

reading or writing is

complete.

STATUS = FCLOSE(FID)

FGETL

Reads a line from a file but discards the newline character.

TLINE = FGETL(FID)

FGETS

Reads a line from a file and keeps the newline character.

TLINE = FGETS(FID)

FREAD

Reads binary data from a file.

[A, COUNT] = FREAD(FID,SIZE,PRECISION)

FWRITE

Writes binary data to a file.

COUNT = FWRITE(FID,A,PRECISION,SKIP)

FPRINTF

Writes formatted data to a file.

COUNT = FPRINTF(FID,FORMAT,A,…)

FSCANF

Reads formatted data from a file.

[A,COUNT] = FSCANF(FID,FORMAT,SIZE)

It is not our intention to present a comprehensive discussion on the different data importing functions available in MATLAB. You can read the MATLAB helps on any of these functions as you come across a need for them. The main points to be made here is that MATLAB supports a host of data formats and provides the low-level functions to let you build a special import function if you need it.

Next post:

Previous post: