CMU
UM


Introduction to M-files

What is an m-file?

An m-file, or script file, is a simple text file where you can place Matlab commands. When the file is run, Matlab reads the commands and executes them exactly as it would if you had typed each command sequentially at the Matlab prompt. All m-file names must end with the extension '.m' (e.g. plot.m). If you create a new m-file with the same name as an existing m-file, Matlab will choose the one which appears first in the path order (help path for more information). To make life easier, choose a name for your m-file which doesn't already exist. To see if a filename.m exists, type help filename at the Matlab prompt.

Why use m-files?

For simple problems, entering your requests at the Matlab prompt is fast and efficient. However, as the number of commands increases or trial and error is done by changing certain variables or values, typing the commands over and over at the Matlab prompt becomes tedious. M-files will be helpful and almost necessary in these cases.

How to create, save or open an m-file?

If you are using PC or Mac:

If you are using Unix:

How to run the m-file?

After the m-file is saved with the name filename.m in the Matlab folder or directory, you can execute the commands in the m-file by simply typing filename at the Matlab prompt.

If you don't want to run the whole m-file, you can just copy the part of m-file that you want to run and paste it at the Matlab prompt.


Use your browser's "Back" button to return to the previous page.

8/29/96 YS