You are on page 1of 2

Saving your work in MATLAB

Saving Data and the Contents of the Command Window

To save your commands and most output in the Command Window (workspace), you can use
the “diary” command. Type “diary” to save subsequent input and output to a text file named
“diary”. To save to a different filename, type “diary filename”, where filename is a name
of your choice. The file may be edited using your favorite text editor (Word, Notepad, etc…) to
remove any mistakes. If the filename for the diary already exists, Matlab will append (add on)
input/output to the end of the file.

To turn off the diary, type “diary off“.

When you open the diary file in a text editor, you should see all input and most output. Note
that you cannot open this file within Matlab and resume working on it.

If you wish to quit Matlab in the middle of a session, and resume working later on, enter the
command “save filename” (filename is a name of your choice) to save variables and their
current values in your workspace to a file filename.mat. This file cannot be edited.

To load the saved variables and values later on, enter the command “load filename”
(assuming the file filename.mat exists).

Note: The command “save” without the file name will save variables/values to a file
matlab.mat. The command “load” without the file name will load from matlab.mat (assuming
the file exists).

To see a list of variables used in the current session, type “who” or “whos”

MATLAB also shows the command history (see the Command History window). You can
save these commands to an M-file (to execute later – see below) or to another text editor.

Important: When you exit Matlab (by typing “quit”, or closing the Matlab window), the
workspace (command window contents, stored variables) is not automatically saved. Matlab will
not prompt you to save your work – it quits immediately.

Using M-files

M-files (script files) provide a better way to save commands. You can enter commands in an M-
file and execute them later as a script file. See p. 443 of the linear algebra text for more details.

To open the M-file editor:

• (If using Matlab 6.1) – Click New → M-File under the File pull-down menu, OR
(If using Matlab 2010a) – Click New → Script under the File pull-down menu
• or click on the New M-File (New Script) icon on the toolbar at the top of the main Matlab
window.

Enter the commands you want to execute, line-by-line. You can also copy and paste commands
from the Command History window.

• To create an M-file from the commands in the Command History window, select all or
some of the commands, right-click on the selection, and click on Create M-File. In the
Command History window, use Ctrl-click to select commands one-by-one; Ctrl+A to
select all commands.

Add comments/documentation by placing a % in front of the comment. MATLAB ignores any


text entered on the line after the %.

To Save and/or Run the script file, press F5. The output from each command in the M-file will
appear in the Command window.

When saving M-files, the file name must begin with a letter, followed by any combination of
letters, digits, and underscores (e.g. “lab1” and “lab_1” are valid names, but “1st_lab” is not
valid). Filenames are not case-sensitive, unlike variables.

If you want Matlab to also display the commands/comments from your M-file in the output, enter
the command “echo on” command at the beginning of the M-file. “echo on” turns on echoing of
commands inside script files. Add the command “echo off” at the end of the M-file to turn off
echoing.

You can also run the script file by entering its filename at the >> prompt in the Command
Window.

Examples of M-files can be found in Appendix A (starting on p. 441) of your linear algebra text.
A specific example is on p. 444.

There is another type of M-file called a function file. This type of file can accept input arguments
and return output arguments. Function files are described briefly on p. 445.

You might also like