Вы находитесь на странице: 1из 4


Documentation
 CONTENTS
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:

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

converted by Web2PDFConvert.com
• 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
dos | factorial | system | unix | winopen

Was this topic helpful? Yes No

Trial Software

Product Updates

MATLAB Documentation
Other Documentation
Support

converted by Web2PDFConvert.com
The Manager's Guide to Solving the Big Data Conundrum
Download white paper

Explore Products

MATLAB
Simulink
Student Software
Hardware Support
File Exchange

Try or Buy

Downloads
Trial Software
Contact Sales
Pricing and Licensing

Learn to Use

Documentation
Tutorials
Examples
Videos and Webinars
Training

Get Support

Installation Help
Answers
Consulting
Application Status
License Center

converted by Web2PDFConvert.com
About MathWorks

Careers
Newsroom
Social Mission
About MathWorks

MathWorks
Accelerating the pace of engineering and science
MathWorks is the leading developer of mathematical computing software for engineers and scientists.
Discover...

 United States

Patents
Trademarks
Privacy Policy
Preventing Piracy
© 1994-2017 The MathWorks, Inc.

Join the conversation

converted by Web2PDFConvert.com

Вам также может понравиться