# 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.

## 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
stuff.mat


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
>> 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
>> 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.

fp = fopen('laser.dat','r');