Programming and Data Types | ![]() ![]() |
Creating M-Files: Accessing Text Editors
M-files are ordinary text files that you create using a text editor. MATLAB provides a built-in editor, although you can use any text editor you like.
Another way to edit an M-file is from the MATLAB command line using the edit
function. For example,
edit
foo
opens the editor on the file foo.m
. Omitting a filename opens the editor on an untitled file.
You can create the fact
function shown on the previous page by opening your text editor, entering the lines shown, and saving the text in a file called fact.m
in your current directory.
Once you've created this file, here are some things you can do:
what
fact.m
type fact
fact
functionfact(5) ans = 120
Note
Save any M-files you create and any MATLAB-supplied M-files that
you edit in a directory that is not in the MATLAB directory tree. If you keep your
files in the MATLAB directory tree, they may be overwritten when you install a
new version of MATLAB. Also note that the locations of files in the MATLAB/
toolbox directory tree are loaded and cached in memory at the beginning of
each MATLAB session to improve performance. If you do save a new or edited
file in the MATLAB/toolbox directory tree, restart MATLAB or use the rehash
function to reload the directory and update the cache before you use the file.
|
![]() | Providing Help for Your Programs | Scripts | ![]() |