• No results found

Saving and Restoring Matlab Information

It is good engineering practice to keep records of calculations. These records can be used for several purposes, including:

• To revise the calculations at a later time.

• To prepare a report on the project.

Matlab provides several methods for saving information from a workspace session. Saving the session output with the diary command and saving and loading the variables with the save and load command are described in this section.

3.2.1 Diary Command

The diary commands allows you to record all of the input and displayed output from aMatlab interactive workspace session. The commands include:

• diary file: Saves all text from the Matlab session, except for the prompts (>>), as text in file, written to the present working directory. If file is not specified, the information is written to the file named diary.

• diary off: Suspends diary operation.

• diary on: Turns diary operation back on.

• diary: Toggles diary state

Example 3.1 Use of diary command

Consider again the example involving roots of the quadratic equation.

Problem: solve for s: s2+ 5s + 6 = 0

s = −b ±√

b2− 4ac 2a Matlab session:

>> diary roots

>> a=1;

>> b=5;

>> c=6;

>> x = -b/(2*a);

>> y = sqrt(b^2-4*a*c)/(2*a);

>> s1 = x+y s1 =

-2

>> s2 = x-y s2 =

-3

The file roots is written in your current working directory. It can be displayed by the Matlab command type roots. It can also be displayed in a Unix terminal window by the command more roots or printed with the command lp roots.

a=1;

b=5;

c=6;

x = -b/(2*a);

y = sqrt(b^2-4*a*c)/(2*a);

s1 = x+y s1 =

-2 s2 = x-y s2 =

-3 diary off

Note that this is nearly the same as the display in the command window, with the exception that theMatlab prompt (>>) is not included.

3.2.2 Saving and Retrieving Matlab Variables

There will be occasions when you want to save yourMatlab variables so that you can later retrieve them to continue your work. In this case, you must save the information in the Matlab binary format, so that the full precision of the variables is retained. Files in this Matlab binary format are known as MAT-files and they have an extension of mat.

Storing and Loading Workspace Values

save Stores workspace values (variable names, sizes, and values), in the binary file matlab.mat in the present working directory

save data Stores all workspace values in the file data.mat

save data 1 x y Stores only the variables x and y in the file data_1.mat

load data 1 Loads the values of the workspace values previously stored in the file data_1.mat

Exporting and Importing Data

There are also situations in which you wish to exportMatlab data to be operated upon with other programs, or to import data created by other programs. This must be done with text files written with save or read with load.

To write a text file data1.dat in the current working directory containing values of Matlab variables in long e format:

save data1.dat -ascii

Note that the individual variables will not be identified with labels or separated in any way. Thus, if you have defined variables a and b in the Matlab workspace, the command above will output first the values in a, then the values in b, with nothing separating them. Thus, it is often desirable to write text files containing the values of only a single variable, such as:

save data2.dat a -ascii

This command causes each row of the array a (more on arrays later) to be written to a separate line in the data file. Array elements on a line are separated by spaces. The separating character can be made a tab with the command:

save data2.dat a -ascii -tab

The .mat extension is not added to an ASCII text file. However, it is recommended that ASCII text file names include the extension .dat so that it is easy to distinguish them from MAT-files and M-files (to be described in the next section).

For an example of importing text data, suppose that a text file named data3.dat contains a set of values that represent the time and corresponding distance of a runner from the starting line in a race. Each time and its corresponding distance value are on a separate line of the data file. The values on a line must be separated by one or more spaces. Consider a data file named data3.dat containing the following:

0.0 0.0

0.1 3.5

0.2 6.8

The load command followed by the filename will read the information into an array with the same name as the base name of the data file (extension removed). For example, the command

load data3.dat

reads the data in the file data3.dat and stores it in the Matlab array variable named data3, having two columns and three rows.

Matlab can also import data from and export data to two common formats that are used in spreadsheet and database program formats. The more common of these file formats is the comma separated values or .csv format. This is an ASCII text file, with values on each line separated by commas, as the name implies. The second format is the spreadsheet .wk1 format, often used as an interchange format for different spreadsheet programs. TheMatlab import and export commands for these formats are:

A = csvread(’file’) Reads a comma separated value formatted file file. The result is returned in A. The file can only contain numeric values.

csvwrite(’file’,A) Writes matrix A into file as comma separated values.

A = wk1read(’file’) Reads all the data from a WK1 spreadsheet file named file into matrix A.

wk1write(’file’,A) Writes matrix A into a WK1 spreadsheet file with the name file.

’.wk1’ is appended to the filename if no extension is given.

Use the help facility for information on options for these commands.