Page 53 - A Guide to MATLAB for Beginners and Experienced Users
P. 53
34 Chapter 3: Interacting with MATLAB
You can remove a variable from the Workspace by selecting it in the Workspace
browser and choosing Edit:Delete.
If you need to interrupt a session and don’t want to be forced to recompute
everything later, then you can save the current Workspace with save.For
example, typing save myfile saves the values of all currently defined vari-
ables in a file called myfile.mat. To save only the values of the variables X
and Y, type
>> save myfile X Y
When you start a new session and want to recover the values of those variables,
use load. For example, typing load myfile restores the values of all the
variables stored in the file myfile.mat.
The Working Directory
New files you create from within MATLAB will be stored in your current
working directory. You may want to change this directory from its default
location, or you may want to maintain different working directories for dif-
ferent projects. To create a new working directory you must use the standard
procedure for creating a directory in your operating system. Then you can
make this directory your current working directory in MATLAB by using cd,
or by selecting this directory in the “Current Directory” box on the Desktop
tool bar.
For example, on a Windows computer, you could create a directory called
C:\ProjectA. Then in MATLAB you would type
>> cd C:\ProjectA
to make it your current working directory. You will then be able to read and
write files in this directory in your current MATLAB session.
If you only need to be able to read files from a certain directory, an alterna-
tive to making it your working directory is to add it to the path of directories
that MATLAB searches to find files. The current working directory and the
directories in your path are the only places MATLAB searches for files, unless
you explicitly type the directory name as part of the file name. To add the
directory C:\ProjectA to your path, type
>> addpath C:\ProjectA
When you add a directory to the path, the files it contains remain available for
the rest of your session regardless of whether you subsequently add another