This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English verison of the page.

Note: This page has been translated by MathWorks. Please click here
To view all translated materals including this page, select Japan from the country navigator on the bottom of this page.

Run External Commands, Scripts, and Programs

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. Use it to invoke utilities or call other executable programs without quitting MATLAB®. To use the exclamation point in a factorial expression, call the factorial function.

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. 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 prompt 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.

For details about running external programs that return results and status, see the unix, dos, and system functions.

    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 Mac platforms, you cannot run AppleScript (from Apple) directly from MATLAB. To run AppleScript commands, call the Apple macOS osascript function using the MATLAB unix or ! (bang) functions.

system Command Blocks Callback Functions

Callback functions are not called while the system command is executing.

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 determine the system path that is visible to MATLAB, type the following at the command prompt:


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 System Path for 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.

  • Type the following commands at the command prompt:

    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 System Path Across MATLAB Sessions Within 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:

    • If you are using bash or a related shell, type:

      export PATH="$PATH:myfolder"
    • If you are using tcsh or a related shell, type:

      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 System 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 

See Also

| | | |

Was this topic helpful?