Saving and loading data

Very often, results of MATLAB calculations need to be reused elsewhere. The values of variables can be saved in a `.mat file'. This can be done with the command
>> save filename
which results in all variables in use being saved in the file with the name `filename.mat'. The extension `.mat' does not necessary need to be given. If only a few variables need to be saved, these variable should be specified after the file name, e.g. only the variables $ x$ and $ y$ are saved in the file filename.mat with the command
>> save filename x y

A saved `.mat' file can be read into MATLAB with the command load

>> load filename
where the extension `.mat' can be omitted. The saved variables and their values are stored in the workspace and can now be used again.

Besides loading data stored in `.mat' files, MATLAB is useful for processing data which is obtained from external sources, e.g., experimental measurements. Typically this data is available as a plain text file organized into columns. MATLAB can easily handle tab or space-delimited text.

There is more than one way to read data into MATLAB from a external data file. The simplest, though least flexible, procedure is to use again the load command to read the entire content of the file in a single step. The load command requires that the data in the file is organized into a rectangular array. No column titles are permitted. One useful form of the load command is:
>> load measurements.txt
where `measurements.txt' is the name of the file containing the data. The result of this operation is that the data in `measurements.txt' is stored in a variable called `measurements'. Files with various extension can be used. Note, that any extension except `.mat' indicates to MATLAB that the data is stored as plain ASCII text. A `.mat' extension is reserved for a file which has stored MATLAB variables.

Suppose you had a simple ASCII file named `meas_xy.txt' that contained two columns of numbers. The following MATLAB statements will load this data into the variable `meas_xy', and then copy it into two vectors, $ x$ and $ y$ .
>> load meas_xy.txt;     %  read data into the meas_xy variable
>> x = meas_xy(:,1);     %  copy first column of my_xy into x
>> y = meas_xy(:,2);     %  and second column into y
After applying these commands, the data stored in column 1 of the text file is stored in the MATLAB variable $ x$ , and the data stored in column 2 of the text file is stored in the MATLAB variable $ y$ . You are now able to process this data with MATLAB.

Another option to import data in MATLAB is to select "File $ \backslash$ Import Data". In the window opening, you can select your data file. Selecting next in this window, various import options are accessable. For example, you can choose whether MATLAB should split columns between spaces, or between commas. Furthermore, you can choose the number of header rows. On the righthand side of your window, you will see a preview how MATLAB will import the data. If you are satisfied, select finish.

Previous      Next      Up      Contents

Esteur 2010-03-22