system
Execute operating system command and return output
Syntax
status = system(command)
(地位、cmdout) =系统(通讯and)
(地位、cmdout) =系统(通讯and,'-echo')
Description
Examples
Input Arguments
Output Arguments
Limitations
MATLAB converts characters to the encoding that your operating system shell accepts. Output from the
command
is converted to the MATLAB encoding to be displayed in the command window. If you get unexpected results from the command, enter thecommand
argument directly at the operating system prompt to see how the operating system treats your input.
MS-DOS®does not support UNC path names. Therefore, if the current folder uses a UNC path name, then running
system
with a DOScommand
that relies on the current folder fails. To work around this limitation, change the folder to a mapped drive before callingsystem
.
Tips
To execute the operating system command in the background, include the trailing character,
&
, in thecommand
argument (for example,'notepad &'
on a Windows platform, or'emacs &'
on UNIX®). The exit status is immediately returned to thestatus
variable. This syntax is useful for console programs that require interactive user command input while they run, and that do not run correctly in the MATLAB Command Window.Note
If
command
includes the trailing&
character,cmdout
is empty.On a UNIX system, the
system
function redirectsstdin
to the invoked command,command
, by default. This redirection also passes MATLAB script commands and the keyboard type-ahead buffer to the invoked command while thesystem
function executes. This behavior can lead to corrupted output whensystem
does not complete execution immediately. To disablestdin
and type-ahead redirection, include the formatted text< /dev/null
in the call to the invoked command.
Algorithms
MATLAB starts a new cmd/shell process, executes the command, exits the process, and returns to the MATLAB process. Updates to the system environment made by the command are not visible to MATLAB.
On UNIX, MATLAB uses a shell program to execute the given command. It determines which shell program to use by checking environment variables on your system. MATLAB first checks theMATLAB_SHELL
variable, and if either empty or not defined, then checksSHELL
. IfSHELL
is also empty or not defined, MATLAB uses/bin/sh
.