Execute operating system command and return output
status = system(command)
[status,cmdout] = system(command)
[status,cmdout] = system(command,'-echo')
On a Windows® system, display the current folder using
command = 'cd'; [status,cmdout] = system(command)
status = 0 cmdout = C:\matlab\myfiles
status of zero indicates that the command
completed successfully. MATLAB returns a character vector containing
the current folder in
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
status of zero indicates that the command completed successfully. MATLAB® returns a list of users in
command— Operating system command
Operating system command, specified as a character vector. The
in a system shell, which might not be the shell from which you started MATLAB.
MATLAB converts characters to the encoding that your operating
system shell accepts. Output from the
converted to the MATLAB encoding to be displayed in the command
window. If you get unexpected results from the command, enter the
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
command that relies on the current folder
fails. To work around this limitation, change the folder to a mapped
drive before calling
To execute the operating system command in the background,
include the trailing character,
&, in the
'notepad &' on a Windows platform,
'emacs &' on UNIX®). The exit status
is immediately returned to the
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.
command includes the trailing
On a UNIX system, the
stdin to the invoked command,
by default. This redirection also passes MATLAB script commands
and the keyboard type-ahead buffer to the invoked command while the
executes. This behavior can lead to corrupted output when
not complete execution immediately. To disable
type-ahead redirection, include the formatted text
< /dev/null in
the call to the invoked command.
You can override an environment variable in the
For example, on a Windows platform, the following code sets the
myPath, then calls the system
dosCommand with that value.
system(['set PATH=' myPath ' && ' dosCommand])
On a UNIX platform, the syntax depends on the shell. For example, using
the BASH shell, the following code sets the
myPath, then calls the system
command with that value.
system(['export PATH=' myPath ' ; ' 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
and if either empty or not defined, then checks
SHELL is also empty or not defined, MATLAB uses