Saving and loading data

>> save filenamewhich results in all variables in use being saved in the file with the name

>> save filename x y

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

>> load filenamewhere 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.txtwhere `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,

>> 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 yAfter applying these commands, the data stored in column 1 of the text file is stored in the MATLAB variable

Another option to import data in MATLAB is to select "File

Esteur 2010-03-22