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.


Execute operating system command and return output


status = system(command)
[status,cmdout] = system(command)
[status,cmdout] = system(command,'-echo')


status = system(command) calls the operating system to execute the specified command. The operation waits for the command to finish execution before returning the exit status of the command to the status variable.


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

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


collapse all

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

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

A status of zero indicates that the command completed successfully. MATLAB returns a character vector containing the current folder in cmdout.

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 = 0

A status of zero indicates that the command completed successfully. MATLAB® returns a list of users in cmdout.

Input Arguments

collapse all

Operating system command, specified as a character vector. The command executes 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 either 0 or a nonzero integer. When the command is successful, status is 0. Otherwise, status is a nonzero integer.

  • If command includes the ampersand character (&), then status is the exit status when command starts

  • If command does not include the ampersand character (&), then status is the exit status upon command completion.

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


  • 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 the command 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 DOS command that relies on the current folder fails. To work around this limitation, change the folder to a mapped drive before calling system.


  • To execute the operating system command in the background, include the trailing character, &, in the command argument (for example, 'notepad &' on a Windows platform, or 'emacs &' on UNIX®). The exit status is immediately returned to the status 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.


    If command includes the trailing & character, cmdout is empty.

  • On a UNIX system, the system function redirects stdin 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 the system function executes. This behavior can lead to corrupted output when system does not complete execution immediately. To disable stdin and type-ahead redirection, include the formatted text < /dev/null in the call to the invoked command.


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 the MATLAB_SHELL variable, and if either empty or not defined, then checks SHELL. If SHELL is also empty or not defined, MATLAB uses /bin/sh.

Introduced before R2006a

Was this topic helpful?