Data Files

Matlab does not allow you to save the commands that you have entered in a session, but it does allow a number of different ways to save the data. In this tutorial we explore the different ways that you can save and read data into a Matlab session.

  1. Saving and Recalling Data
  2. Saving a Session as Text
  3. C Style Read/Write

Saving and Recalling Data

As you work through a session you generate vectors and matrices. The next question is how do you save your work? Here we focus on how to save and recall data from a Matlab session. The command to save all of the data in a session is save. The command to bring the data set in a data file back into a session is load.

We first look at the save command. In the example below we use the most basic form which will save all of the data present in a session. Here we save all of the data in a file called “stuff.mat.” (.mat is the default extension for Matlab data.)

>> u = [1 3 -4];
>> v = [2 -1 7];
>> whos
  Name      Size                    Bytes  Class

  u         1x3                        24  double array
  v         1x3                        24  double array

Grand total is 6 elements using 48 bytes

>> save stuff.mat
>> ls

The ls command is used to list all of the files in the current directory. In this situation we created a file called “stuff.mat” which contains the vectors u and v. The data can be read back in to a Matlab session with the load command.

>> clear
>> whos
>> load stuff.mat
>> whos
  Name      Size                    Bytes  Class

  u         1x3                        24  double array
  v         1x3                        24  double array

Grand total is 6 elements using 48 bytes

>> u+v

ans =

     3     2     3

In this example the current data space is cleared of all variables. The contents of the entire data file, “stuff.mat,” is then read back into memory. You do not have to load all of the contents of the file into memory. After you specify the file name you then list the variables that you want to load separated by spaces. In the following example only the variable u will be loaded into memory.

>> clear
>> whos
>> load stuff.mat u
>> whos
  Name      Size                    Bytes  Class

  u         1x3                        24  double array

Grand total is 3 elements using 24 bytes

>> u

u =

     1     3    -4

Note that the save command works in exactly the same way. If you only want to save a couple of variables you list the variables you want to save after the file name. Again, the variables must be separated by a space. For an example and more details please see the help file for save. When in matlab just type in help save to see more information. You will find that there are large number of options in terms of how the data can be saved and the format of the data file.

Saving a Session as Text

Matlab allows you to save the data generated in a session, but you cannot easily save the commands so that they can be used in an executable file (Executable Files). You can save a copy of what happened in a session using the diary command. This is very useful if you want to save a session for a homework assignment or as a way to take notes.

A diary of a session is initiated with the diary command followed by the file name that you want to keep the text file. You then type in all of the necessary commands. When you are done enter the diary command alone, and it will write all of the output to the file and close the file. In the example below a file called “save.txt” is created that will contain a copy of the session.

>> diary save.txt
        ... enter commands here...
>> diary

This will create a file called “save.txt” which will hold an exact copy of the output from your session. This is how I generated the files used in these tutorials.

C Style Read/Write

In addition to the high level read/write commands detailed above, Matlab allows C style file access. This is extremely helpful since the output generated by many home grown programs is in binary format due to disk space considerations. This is an advanced subject, and we do not go into great detail here. Instead we look at the basic commands. After looking at this overview we highly recommend that you look through the relevant help files. This will help fill in the missing blanks.

The basic idea is that you open a file, execute the relevant reads and writes on a file, and then close a file. One other common task is to move the file pointer to point to a particular place in the file, and there are two commands, fseek and ftell to help.

Here we give a very simple example. In the example, a file called “laser.dat” is opened. The file identifier is kept track of using a variable called fp. Once the file is opened the file position is moved to a particular place in the file, denoted pos , and two double precision numbers are read. Once that is done the position within the file is stored, and the file is closed.

fp = fopen('laser.dat','r');
tmp = fread(fp,2,'double');
pos = ftell(fp);