Programming Previous page   Next Page

Program Development

This section explains the basic steps in developing an M-file program in MATLAB. Associated with each step in this process are certain MATLAB tools and utilities that are fully documented in the Desktop Tools and Development Environment documentation.

This section covers

For more ideas on good programming style, see Program Development. The MATLAB Programming Tips is a compilation of useful pieces of information that can show you alternate and often more efficient ways to accomplish common programming tasks while also expanding your knowledge of MATLAB.

Creating a Program

You can type in your program code using any text editor. This section focuses on using the MATLAB Editor/Debugger for this purpose. The Editor/Debugger is fully documented in Ways to Edit and Debug Files in the Desktop Tools and Development Environment documentation.

The first step in creating a program is to open an editing window. To create a new M-file, type the word edit at the MATLAB command prompt. To edit an existing M-file, type edit followed by the filename:

MATLAB opens a new window for entering your program code. As you type in your program, MATLAB keeps track of the line numbers in the left column.

Saving the Program

It is usually a good idea to save your program periodically while you are in the development process. To do this, click File -> Save in the Editor/Debugger. Enter a filename with a .m extension in the Save file as dialog box that appears and click OK. It is customary and less confusing if you give the M-file the same name as the first function in the M-file.

Running the Program

Before trying to run your program, make sure that its M-file is on the MATLAB path. The MATLAB path defines those directories that you want MATLAB to know about when executing M-files. The path includes all the directories that contain functions provided with MATLAB. It should also include any directories that you use for your own functions.

Use the which function to see if your program is on the path:

If not, add its directory to the path using the addpath function:

Now you can run the program just by typing the name of the M-file at the MATLAB command prompt:


Previous page  M-File Programming Getting the Bugs Out Next page

© 1994-2005 The MathWorks, Inc.