Data Files
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
Bytes 24 24
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 load stuff.mat whos Name Size u v 1x3 1x3
Bytes 24 24
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 u 1x3
Bytes 24
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.
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'); fseek(fp,pos,'bof'); tmp = fread(fp,2,'double'); pos = ftell(fp); fclose(fp);