Skip to Main Content Skip to Search
Home |   Select Country  Choose Country  |  Contact Us  |  Cart Store 
Create Account | Log In
Products & Services Solutions Academia Support User Community Company
spacer spacer spacer spacer spacer spacer

 

Student Center

Working with Files, Directories, and Paths


Introduction
As we have seen there are a number of different files associated with MATLAB that need to be managed, script files (M-files), data files (MAT-files), and diary files. And with so many uses of files in MATLAB, it is important to understand how files are managed, organized, and accessed in MATLAB.

In general, 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.

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, and the floppy diskette drive is the "A" 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). For example, in the A drive there might be a directory called "my_stuff" within which is a subdirectory called "letters"; inside "letters" there could be many text files of letters, for example "letter_to_mom.txt" and "information_request.txt". If we wanted to open up the file "letter_to_mom.txt" in some text editor we need to make sure that the text editor knows where this file is located. The way we usually locate and open a file is by using a file opening dialog box, File->Open.

In MATLAB the way that we find a single file to edit is the same as in any other text editor. However, MATLAB needs to do more than just find and edit a single text file. MATLAB needs to be able to locate all the different M-files, MAT-files, etc., and implement them correctly at the same time (i.e. It needs a list of all the paths of where different files live so that they can be accessed.).

How MATLAB Finds Files
Let's get back to the issue of how and where MATLAB finds files. You may already know that MATLAB always look inside your present working directory (type, pwd,at the MATLAB command prompt to see your present working directory) first for any files it needs. However, if the file is not located in the present working directory MATLAB will also search in other directories as well.

These other directories that MATLAB will search through are stored in the MATLAB path; the MATLAB path is simply a list of complete paths to directories which MATLAB searches through to find a given file. Even the present working directory can be thought of as part of the MATLAB path. To print out the current MATLAB path type, matlabpath or path, at the command prompt:

matlabpath

MATLABPATH

k:\matlab51\toolbox\matlab\general
k:\matlab51\toolbox\matlab\ops
k:\matlab51\toolbox\matlab\lang
k:\matlab51\toolbox\matlab\elmat
k:\matlab51\toolbox\matlab\elfun
.....
.....etc.
.....
k:\matlab51\toolbox\simulink\blocks
k:\matlab51\toolbox\simulink\simdemos
k:\matlab51\toolbox\simulink\dee
k:\matlab51\toolbox\local

In looking for a file MATLAB actually searches first in the present working directory, and then search from the top of the list, to the bottom, until the file is located. If it is not found MATLAB will produce an error message.

nonexistent_file
??? Undefined function or variable nonexisitent_file'.

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 add the complete path to your directory to the MATLAB path. There are two ways you can append your own paths to the MATLAB path: 1) use the addpath command - type addpath followed by the complete path to your directory, or 2) use the path tool of MATLAB - type pathtool at the command prompt, or select File->Set Path…

For example, to have MATLAB be able to find files in the directory "a:\my_stuff\letters" you would add it to the MATLAB path.

type letter_to_mom.txt
??? Error using ==> type
letter_to_mom.txt: File not found.

addpath a:\my_stuff\letters
matlabpath

MATLABPATH

a:\my_stuff\letters
k:\matlab51\toolbox\matlab\general
k:\matlab51\toolbox\matlab\ops
k:\matlab51\toolbox\matlab\lang
k:\matlab51\toolbox\matlab\elmat
k:\matlab51\toolbox\matlab\elfun
k:\matlab51\toolbox\matlab\specfun
.....
.....etc.
.....
k:\matlab51\toolbox\simulink\simdemos
k:\matlab51\toolbox\simulink\dee
k:\matlab51\toolbox\local
type letter_to_mom.txt

Hi mom!

Useful Function When Working With Paths
We have already introduced some useful functions, but below is a more complete list of helpful functions:

pwd - present working directory
dir, or ls - List directory
what - List MATLAB-specific files in directory
cd - Change current working directory

path, or matlabpath - List the MATLAB search path
addpath - Add directory to search path
pathtool - Invoke the path tool interface

help general - List of general MATLAB commands

Below are some examples of there use: cd c:\my_scripts
pwd

ans =
c:\my_scripts

dir

. .. script1.m

cd a:\my_stuff\letters
pwd

ans =
a:\my_stuff\letters

dir

. information_request.txt
.. letter_to_mom.txt

type letter_to_mom.txt

Hi mom!

Some additional concepts that you may wish to look into in the content tab of the MATLAB help browser:

- Programming and Data Types: M-File Programming: Flow Control
- Programming and Data Types: M-File Programming: Functions
- Graphics: Creating Specialized Plots
- Graphics: Printing and Exporting

You have now completed the MATLAB tutorial. Congratulations!

See also: Simulink Tutorial

Contact sales
Software evaluation for instructors
Free technical kit
E-mail this page

MATLAB & Simulink Student Version

Includes full-featured versions of both MATLAB and Simulink

learn moreLearn more
buy nowBuy now