Documentation

system

Execute operating system command and return output

Syntax

status = system(command)
(地位、cmdout) =系统(通讯and)
(地位、cmdout) =系统(通讯and,'-echo')

Description

status= system(command)调用操作系统执行specified command. The operation waits for the command to finish execution before returning the exit status of the command to thestatusvariable.

example

[status,cmdout] = system(command)also returns the output of the command tocmdout. This syntax is most useful for commands that do not require user input, such asdir.

[status,cmdout] = system(command,'-echo')also displays (echoes) the command output in the MATLAB®Command Window. This syntax is most useful for commands that require user input and that run correctly in the MATLAB Command Window.

Examples

collapse all

On a Windows®system, display the current folder using thecdcommand.

command ='cd'; [status,cmdout] = system(command)
status = 0 cmdout = C:\matlab\myfiles

Astatusof zero indicates that the command completed successfully. MATLAB returns a character vector containing the current folder incmdout.

List all users who are currently logged in, and save the command exit status and output. Then, view the status.

command ='who'; [status,cmdout] = system(command); status
status = 0

Astatusof zero indicates that the command completed successfully. MATLAB® returns a list of users incmdout.

Input Arguments

collapse all

Operating system command, specified as a character vector. Thecommandexecutes in a system shell, which might not be the shell from which you started MATLAB.

Example:'dir'

Example:'ls'

Output Arguments

collapse all

Command exit status, returned as either0or a nonzero integer. When the command is successful,statusis0. Otherwise,statusis a nonzero integer.

  • Ifcommandincludes the ampersand character (&), thenstatusis the exit status whencommandstarts

  • Ifcommanddoes not include the ampersand character (&), thenstatusis the exit status uponcommandcompletion.

Output of the operating system command, returned as a character vector. The system shell might not properly represent non-Unicode®characters.

Limitations

  • MATLAB converts characters to the encoding that your operating system shell accepts. Output from thecommandis converted to the MATLAB encoding to be displayed in the command window. If you get unexpected results from the command, enter thecommandargument 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 runningsystemwith a DOScommandthat 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 thecommandargument (for example,'notepad &'on a Windows platform, or'emacs &'on UNIX®). The exit status is immediately returned to thestatusvariable. 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

    Ifcommandincludes the trailing&character,cmdoutis empty.

  • On a UNIX system, thesystemfunction redirectsstdinto the invoked command,command, by default. This redirection also passes MATLAB script commands and the keyboard type-ahead buffer to the invoked command while thesystemfunction executes. This behavior can lead to corrupted output whensystemdoes not complete execution immediately. To disablestdinand type-ahead redirection, include the formatted text< /dev/nullin 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_SHELLvariable, and if either empty or not defined, then checksSHELL. IfSHELLis also empty or not defined, MATLAB uses/bin/sh.

Introduced before R2006a

Was this topic helpful?