Run External Commands, Scripts, and 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® platforms, for example, the following code invokes the vi editor for a file named yearlystats.m:

!vi 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 Microsoft® Excel® software and returns control to the Command Window so you can continue running MATLAB language statements.

Restrictions maintained within the operating system determine the maximum length of the argument list you can provide as input to the bang (!) command. If you are running the Microsoft Windows XP operating system, for example, the length of the argument list input to the bang command cannot exceed 8189 characters.

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

    Note   To execute operating system commands with specific environment variables, include all commands to the operating system within the system call. Separate the commands using & (ampersand) for DOS, and ; (semicolon) for UNIX platforms. This applies to the MATLAB ! (bang), dos, unix, and system functions. Another approach is to set environment variables before starting MATLAB.

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

Running UNIX Programs That Are Off the System Path

You can run a UNIX program from MATLAB when the folder containing that file is not on the UNIX system path that is visible to MATLAB. To determine the system path that is visible to MATLAB, type the following in the Command Window:

getenv('PATH')

You can make modifications to the system path that persist for the current MATLAB session or across subsequent MATLAB sessions, as described in the sections that follow.

Modify the System Path for the Current MATLAB Session

Do one of the following:

  • Change the current folder in MATLAB to the folder that contains the program you want to run.

  • Issue these commands using the Command Window:

    path1 = getenv('PATH')
    path1 = [path1 ':/usr/local/bin']
    setenv('PATH', path1)
    !echo $PATH 

If you restart MATLAB, the folder is no longer on the system path visible to MATLAB.

Modify the System Path Across MATLAB Sessions Within the Current Shell Session

To add a folder to the system path from the shell:

  1. Stop MATLAB.

  2. Depending on the shell you are using, type one of the following at the system command prompt, where myfolder is the folder that contains the program you want to run:

    • Type this if you are using bash or a related shell:

      export PATH="$PATH:myfolder"
      
    • Type this if you are using tcsh or a related shell:

      setenv PATH "${PATH}:myfolder"
      
  3. Start MATLAB.

  4. In the MATLAB Command Window, type:

    !echo $PATH
    

If you restart MATLAB within the current shell session, the folder remains on the system path visible to MATLAB. However, if you restart the shell session, and then restart MATLAB, the folder is no longer on the system path visible to MATLAB.

Modify the System Path Across All MATLAB Sessions

To make adjustments that persist across shell and MATLAB sessions, add the following commands to the MATLAB startup file as described in Specifying Startup Options in MATLAB Startup File:

path1 = getenv('PATH')
path1 = [path1 ':/usr/local/bin']
setenv('PATH', path1)
!echo $PATH 
Was this topic helpful?