Desktop Tools and Development Environment Previous page   Next Page

Running Functions and Programs, and Entering Variables

Running Statements at the Command Line Prompt

Entering Variables and Running Functions

At the prompt, enter data and run functions. For example, to create A, a 3-by-3 matrix, type

A = [1 2 3; 4 5 6; 7 8 10]

When you press the Enter or Return key after typing the line, MATLAB responds with

A =
     1     2     3
     4     5     6
     7     8    10

To run a function, type the function including all arguments and press Enter or Return. MATLAB displays the result. For example, type


and MATLAB returns

ans =
    1     3
    4     2

Definition of a Statement.   All of the information you type before pressing Enter or Return is known as a statement. This can include:

Some functions support a form that does not require an input argument, thereby operating as commands. For convenience, the term function is used to refer to both functions and commands.

When you enter program control statements, such as if ... end, the prompt does not appear until you complete the set of functions. In the following example, you press Enter at the end of each line, but the prompt does not appear until you complete the set of statements with end.

Image of MATLAB Command Window showing if through end statements on multiple lines.

Running M-Files

Run M-files, files that contain code in the MATLAB language, the same way that you would run any other MATLAB function. Type the name of the M-file in the Command Window and press Enter or Return. The M-file must be in the MATLAB current directory or on the MATLAB search path--for details, see Search Path. You can also use the run function and specify the full pathname to an M-file script.

To determine the name of the M-file currently running, use mfilename.

Examining Errors

If an error message appears when you run an M-file, click the underlined portion of the error message, or position the cursor within the filename and press Ctrl+Enter. The offending M-file opens in the Editor/Debugger, scrolled to the line containing the error.

Processing Order

In MATLAB, you can only run one process at a time. If MATLAB is busy running one function, any further statements you issue are buffered in a queue. The next statement will run when the previous one finishes.

Interrupting a Running Program

You can stop a running program by pressing Ctrl+C or Ctrl+Break at any time. On Macintosh platforms, you can also use Command+. (Command key and the period key) to stop the program. For certain operations, this might generate errors in the Command Window.

For M-files that run for a long time, or that call built-ins or MEX-files that take a long time, Ctrl+C does not always effectively stop execution. In that event, include a drawnow command in your M-file, for example, within a large loop. Note that Ctrl+C might be less responsive if you started MATLAB with the -nodesktop option.

Previous page  Opening the Command Window Running External Programs Next page

© 1994-2005 The MathWorks, Inc.