system

Runs an operating-system command and returns its exit status and output.

Category: Environment and Settings > System Commands

Syntax

status = system(cmd)

Runs cmd, shows its output and returns the exit status.

[status, out] = system(cmd)

Captures the output in out instead of showing it.

[status, out] = system(cmd, '-echo')

Captures the output and also shows it.

Description

The string is handed to the shell of the host, so shell syntax (pipes, redirection, built-in commands) is available. A failing command does not raise a MATLAB error: check status. Environment variables set with setenv are seen by the command.

Use cmdsep to join several commands in a portable way.

Input arguments

cmd
Command line passed to the system shell.
'-echo'
Show the output even when it is also captured.

Output arguments

status
Exit code; 0 means success, any other value means the command failed.
out
Character vector with the text the command wrote, including standard error.

Example

Capture output and status

[status, out] = system('echo hello');
disp(status)
disp(strtrim(out))
status = system('exit 3');
disp(status)
Output
     0
hello
     3

See also

dos unix perl cmdsep setenv