Desktop Tools and Development Environment Previous page   Next Page

Running External Programs

The exclamation point character, !, sometimes called bang, is a shell escape and indicates that the rest of the input line is a command to the operating system. Use it to invoke utilities or call other executable programs without quitting MATLAB. On UNIX, for example,

!vi yearlystats.m

invokes the vi editor for a file named yearlystats.m. After the external program completes or you quit the program, the operating system returns control to MATLAB. Add & to the end of the line, such as

!dir &

on Windows platforms to display the output in a separate window or to run the application in background mode. For example

!excel.exe &

opens Excel and returns control to the Command Window so you can continue running MATLAB statements.

See the reference pages for the unix, dos, and system functions for details about running external programs that return results and status.

On Macintosh platforms, you cannot run AppleScript directly from MATLAB. However, you can run the Macintosh OS X osascript function from the MATLAB unix or ! (bang) function to run AppleScript from MATLAB.

UNIX System Path and Running UNIX Programs from MATLAB

To run a UNIX program from MATLAB if its directory is not on the UNIX system path MATLAB uses, take one of the actions described here.

Change Current Directory in MATLAB.   Change the current directory in MATLAB to the directory that contains the program you want to run.

Modify the UNIX System Path MATLAB Uses.   Add the directories to the system path from the shell. The exact steps depend on your shell. This is an example using sh:

  1. At the system command prompt, type
  1. where <mydirectory> is the directory that contains the program you want to run.

  1. Start MATLAB.
  2. In MATLAB, type

The directory containing the file is added to the system path that MATLAB uses. This change applies only to the current session of the terminal window.

Automatically Modify System Path at MATLAB Startup.   If you want to add a directory to the PATH environment variable each time you start MATLAB, perform these steps:

  1. In a text editor, open the file MATLAB/bin/matlab. This file is used to start MATLAB.
  2. Add this line to the beginning of the matlab file
  1. where <mydirectory> is the directory you want to add to the path.

    If you run a tsch shell instead of a bash shell, use setenv instead of export.

  1. Save the file.

The matlab file will modify the PATH environment variable, and then start MATLAB.

Evaluating or Opening a Selection

Make a selection in the Command Window and press Enter or Return. The selection is appended to whatever is at the prompt, and MATLAB executes it.

Similarly, you can select a statement from any MATLAB desktop tool, right-click, and select Evaluate Selection from the context menu. Alternatively, after making a selection, use the shortcut key, F9, or for some tools, press Enter or Return. For example, you can scroll up in the Command Window, select a statement you entered previously, and then press Enter to run it. If you try to evaluate a selection while MATLAB is busy, for example, running an M-file, execution waits until the current operation is done.

You can open a function, file, variable, or Simulink® model from the Command Window. Select the name in the Command Window, and then right-click and select Open Selection from the context window. This runs the open function for the item you selected so that it opens in the appropriate tool:

See the open reference page for details about what action occurs if there are name conflicts. If no action exists to work with the selected item, Open selection calls edit.

Function Alternative

Use open or edit to open a file in the Editor/Debugger. Use type to display the M-file in the Command Window.

Hyperlinks for Running Functions

Use matlab: to run a specified statement when you click a hyperlink in the Command Window. For example

disp('<a href="matlab:magic(4)">Generate magic square</a>')

displays

Image of hyperlink in Command Window. The hyperlink text is Generate magic square. Visual cues it is a hyperlink are the text is a blue color and is underlined.

When you click the link Generate magic square, MATLAB runs magic(4). Alternatively, you can press Ctrl+Enter if the cursor is positioned in the link text. You can use the disp, error, fprintf, or warning function with this feature. Change the hyperlink color using Display preferences for the Command Window--see Preferences for the Command Window. For more information, including examples, see the matlabcolon (matlab:) reference page.


Previous page  Running Functions and Programs, and Entering Variables Controlling Input Next page

© 1994-2005 The MathWorks, Inc.