In MATLAB, commands, variables, and command window sessions are commonly stored inside files. For example, the next lesson will talk about storing sequences of commands and functions in files called scripts which can be run in the MATLAB command window. MATLAB has other commands, which will be explained in a later lesson, for saving the variables you have defined, and for loading them back in for use in a later session. You can even save, verbatim, the contents of all or part of the MATLAB command window (i.e., the text that you typed and that MATLAB returned as results) in a file. With so many uses of files in MATLAB, it is important to understand how files are managed, organized, and accessed in MATLAB.
Files are managed, organized, and accessed in MATLAB in the same way as in Microsoft Windows, that is, in a hierarchical file system. In fact, you can access all of your Windows files from within MATLAB (although, the MATLAB interface to the files is different than Window's --- for those of you that know DOS, MATLAB's interface to files is essentially the same as DOS's). You should already understand how files are organized, managed, and accessed in Windows. However, to review briefly, inside of the computer you are using are one or more disk drives which are named with letters; the primary hard disk is usually named the "C" drive (If you want to see all of your machine's drives, double click on the “My Computer” icon at the top left of your screen). All of your files are stored on these disk drives, and they are organized hierarchically by being stored inside named directories and subdirectories (also called folders and subfolders). If this brief review is confusing to you, you should go and review basic use of Microsoft Windows and its file interface.
Paths and Directories
A path is a text string telling how to get to a particular file or directory. A path can also refer to a directory. You can always find out what the present working directory is by using the "pwd" command. For example:
Go ahead and try this now in your own command window: type "pwd" and then hit enter.
Listing Directory Contents
Another useful command is the "dir" command. The "dir" command will list all of the files and directories inside of the present working directory. For example, typing "dir" may return something “LIKE” the following:
Go ahead and try the "dir" command yourself now. While "dir" lists all files in the present working directory, the related command "what" lists only MATLAB-specific files in the present working directory.
Navigating Directories and More about Paths
The "cd" command allows you to change directory (i.e., using "cd" you can navigate through the directory structure of your computer's drives). After typing "cd" at the command prompt, you can either type the complete path (i.e., starting with the drive letter) to the directory where you want to move, or a relative path (i.e., one that doesn't start with a drive letter) that will simply be appended to the end of the path of the present working directory to form a complete path to move to (i.e., it is relative to the present working directory). For example, to change the working directory to a directory "C:\my_scripts" and list the files there:
Of course the easiest way to navigate around your available system storage is to simply use the drop-down menu at the top of the MATLAB command window (note the arrows which point out places for your to click upon to find out where you are located and help you figure out how to get to where you want to store your files … like on a USB supported thumb drive, for example:
How MATLAB Finds Files
Let's get back to the issue of how and where MATLAB finds files. You learned above that MATLAB will always look inside the present working directory first for any files it needs. However, MATLAB will also search in other directories as well if it doesn't find a file in the present working directory. These other directories that MATLAB will search are stored in the matlabpath; the matlabpath is simply a list of complete paths to directories, and if MATLAB doesn't find a file in the present working directory it will search these directories in order to try to find the file. You can print out the current matlabpath by typing "matlabpath" at the command prompt ("matlabpath" is also known as just "path" and so just typing "path" will return the same thing):
So, what do you do if you want to store your MATLAB files in some directory of your choosing, but still have MATLAB be able to find your files? Simple, you can add the complete path to your directory to matlabpath. This is done with the "addpath" command; type "addpath" followed by the complete path to your directory at the command prompt, and MATLAB will then search your directory as well as the ones listed above. It will search the directory you added to matlabpath before it searches any of the other paths, and you can add as many directories as you want to matlabpath. For example, to have MATLAB be able to find files in the directory "a:\my_stuff\letters" you would add it to matlabpath:
» type letter_to_mom.txt
To continue on to the next lesson, click here.