Skip to content
Hub Nexus
Updated

AuthorNo author yetClaim it

See something to improve? Propose a change.

Support

This page is public.

The Current Directory and Defined Path

It is necessary to declare a current directory before saving a file, loading a file, or running an M-file. By default, unless you edit the MATLAB shortcut, the current directory will be .../MATLAB/work. After you start MATLAB, change the current directory by either using the toolbar at the left-hand side of the screen, or entering the path in the bar at the top.

The current directory is the directory MATLAB will look in first for a function you try to call. Therefore if you have multiple folders and each of them has an M-file of the same name, there will not be a discrepancy if you set the current directory beforehand. The current directory is also the directory in which MATLAB will first look for a data file.

If you still want to call a function but it is not part of the current directory, you must define it using MATLAB's 'set path' utility. To access this utility, follow the path:

file > set path... > add folder...

You could also go to "add folder with subfolders...", if you're adding an entire group, as you would if you were installing a toolbox. Then look for and select the folder you want. If you forget to do this and attempt to access a file that is not part of your defined path list, you will get an 'undefined function' error.

Saving Files

There are many ways to save to files in MATLAB.

  • save - saves data to files, *.mat by default
  • uisave - includes user interface
  • hgsave - saves figures to files, *.fig by default
  • diary [filename] - saves all the text input in the command window to a text file.

All of them use the syntax:

save filename.ext

or similar for the other functions. The files are saved in your current directory, as seen on the top of the window. By default the current directory is .../MATLAB/work.

Loading Files

Likewise, there are many ways to load files into the workspace. One way is to use the "file" menu. To open a .m file click "open", whereas to import data from a data file select "import data..." and follow the wizard's instructions.

An alternative way to load a saved .mat file (within a function, for example) is to type:

>> load filename.ext

The file must be in a recognized directory (usually your current directory, but at least one for which the path has been set).

The data in the .mat file is stored with the same name as the variable originally had when it was saved. To get the name of this and all other environment variables, type "who".

To open an .m file, you can use file -> open, or type

>>open filename.ext

File Naming Constraints

You can name files whatever you want (usually simpler is better though), with a few exceptions:

  • MATLAB for Windows retains the file naming constraints set by DOS. The following characters cannot be used in filenames:

" / : * < > | ?

  • You're not allowed to use the name of a reserved word as the name of a file. For example, while.m is not a valid file name because while is one of MATLAB's reserved words.
  • When you declare an m-file function, the m-file must be the same name as the function or MATLAB will not be able to run it. For example, if you declare a function called 'factorial':

function Y = factorial(X)

You must save it as "factorial.m" in order to use it. MATLAB will name it for you if you save it after typing the function declaration, but if you change the name of the function you must change the name of the file manually, and vice versa.

Where this page came from

This page was imported from Wikibooks. From “MATLAB Programming” on Wikibooks, by its contributors, under CC BY-SA 4.0. Changed here: set as a page; navigation and edit links left out; each image under its own licence, credited in its caption.

Nobody has written it yet — it is the source material at a new address, which is why search engines are asked to skip it and why no one earns from it. It is up for grabs: take it on, and it is yours to rewrite and to earn from.

In these publicationsMATLAB Programming

LanguagesEnglish

Licence: CC BY-SA 4.0 · Adapted from en.wikibooks.org

Views

Comments

Spinner Logo
Version: 2CC0 1.0 (public domain)
The runaway star that left the Tarantula Nebula
Version: 2CC0 1.0 (public domain)
The Eagle Nebula, seen in the infrared
Version: 2CC0 1.0 (public domain)
The Cosmic Cliffs are not cliffs
Version: 2CC0 1.0 (public domain)
The recommended targets are real, but the largest health gain is between doing nothing and doing a little. That is the part worth knowing if you are starting from nothing.
Version: 2CC0 1.0 (public domain)
Sleep is not one state but a cycle through several, each doing different work. Most sleep disorders are a failure to complete that cycle rather than a shortage of hours.
Version: 2CC0 1.0 (public domain)
Four nebulae that revealed the shape of our galaxy