Main Content

Run External Commands, Scripts, and Programs

You can execute operating system commands from the MATLAB® command line using the ! operator or the system function.

Shell Escape Function

The exclamation point character (!), sometimes called bang, is a shell escape. The ! character indicates that the rest of the input line is a command to the operating system. The operating system determines the maximum length of the argument list you can provide as input to the command. Use ! to call utilities or other executable programs without quitting MATLAB.

For example, the following code opens the vi editor for a file named yearlystats.m on a UNIX® platform.

!vi yearlystats.m

After the external program completes or you quit the program, the operating system returns control to MATLAB. To run the application in background mode or display the output in a separate window, add & to the end of the line.

For example, the following statement opens the Microsoft® Excel® program and returns control to the command prompt so that you can continue running MATLAB commands.

!excel.exe &

The following command on a Windows® platform displays the results in a DOS window.

!dir &

Note

To use the exclamation point in a factorial expression, call the factorial function.

Return Results and Status

To run a program that returns results and status, use the system function.

Specify Environment Variables

To execute operating system commands with specific environment variables, include all commands to the operating system within the system call. This applies to the MATLAB ! (bang), system, dos, and unix functions. To separate commands:

  • On Windows platforms, use & (ampersand)

  • On UNIX platforms, use ; (semicolon)

Alternatively, set environment variables before starting MATLAB.

Run UNIX Programs off 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 view the path visible to MATLAB, type the following at the MATLAB command prompt.

getenv('PATH')

You can modify the system path for the current MATLAB session or across subsequent MATLAB sessions, as described in these topics:

Current MATLAB Session

You can modify the system path for the current MATLAB session. When you restart MATLAB, the folder is no longer on the system path.

To modify the system path, do one of the following.

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

  • Type the following commands at the command prompt.

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

Across MATLAB Sessions Within Current Shell Session

You can modify the system path within a shell session. When you restart MATLAB within the current shell session, the folder remains on the system path. However, if you restart the shell session, and then restart MATLAB, the folder is no longer on the path.

To add a folder to the system path from the shell, do the following.

  1. Exit 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:

    • For bash or related shell:

      export PATH="$PATH:myfolder"
      
    • For tcsh or related shell:

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

  4. In the MATLAB Command Window, type:

    !echo $PATH
    

Across All MATLAB Sessions

To modify the system path across shell and MATLAB sessions, add the following commands to the MATLAB startup file as described in Startup Options in MATLAB Startup File.

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

Run AppleScript on macOS

On macOS platforms, you cannot run the Apple AppleScript program directly from MATLAB. To run AppleScript commands, call the Apple macOS osascript function using the MATLAB unix or ! (bang) functions.

See Also