Quantcast

Documentation Center

  • Trial Software
  • Product Updates

dos

Execute DOS command and return output

Syntax

  • status = dos(command) example
  • [status,cmdout] = dos(command) example
  • [status,cmdout] = dos(command,'-echo') example

Description

example

status = dos(command) executes the specified DOS command for Windows® platforms, and waits for the command to finish execution before returning the exit status to the status variable.

example

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

example

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

Examples

expand all

Save DOS Command Exit Status

Issue a DOS command to create a new folder named mynew and save the exit status to a variable.

command = 'mkdir mynew';
status = dos(command)
status =

     0

The status of zero indicates that the mynew folder was created successfully.

Open and Run a Windows UI Command

Open Notepad and immediately return the exit status to MATLAB by appending an ampersand (&) to the notepad command.

status = dos('notepad &')
status =

     0

The status of zero indicates that Notepad successfully started.

Save Successful DOS Command Status and Output

Execute the DOS command, dir, and view the exit status and command output.

[status,cmdout] = dos('dir');
status, cmdout
status =

     0

cmdout =

 Volume in drive C is OSDisk
 Volume Serial Number is XXX-XXXX

 Directory of C:\my_MATLAB_files

04/10/2012  12:08 PM <DIR>     				.
04/10/2012  12:08 PM <DIR>     				..
04/21/2011  09:24 AM               171 base.mat
02/08/2010  05:14 PM                73 baseball.dat
04/10/2012  12:08 PM               474 collatz.asv
04/10/2012  11:56 AM               480 collatz.m
.
.
.

When you issue a valid DOS command, status indicates success and cmdout contains the command output.

Save Unsuccessful DOS Command Status and Output

Attempt to execute a command called foo. Then, view the status and results output arguments.

[status,results] = dos('foo');
status, results
status =

     1


results =

'foo' is not recognized as an internal or external command,
operable program or batch file.

When you issue an invalid DOS command, status indicates failure and results contains the DOS error message.

Display DOS Command Output in MATLAB Command Window

Display command output and prompts in the Command Window as the command executes, and also assign the command output to the results variable.

[status,results] = dos('comp', '-echo');
Name of first file to compare: collatz.m
collatz.m 
Name of second file to compare: collatz.asv
collatz.asv 
Option: /A
/A 
Option: 
 
Comparing collatz.m and collatz.asv... 
Files compare OK 
 
Compare more files (Y/N) ? N
N 
>>

Input Arguments

expand all

command — MS-DOS® commandstring

MS-DOS command, specified as a string. The command can be a Windows UI program that opens a graphical user interface, or a DOS console command that you typically run in a DOS command window. The command executes in a DOS shell, which might not be the shell from which you launched MATLAB.

Example: 'dir'

Output Arguments

expand all

status — Command exit status0 | nonzero integer

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 upon command launch.

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

cmdout — Output of operating system commandstring

Output of the operating system command, returned as a string.

Limitations

  • DOS does not support UNC path names. Therefore, if the current folder uses a UNC path name, then running dos with a DOS command that relies on the current folder fails. MATLAB returns this error:

    Error using dos
    DOS commands may not be executed when the current directory is a UNC pathname

    To work around this limitation, change the folder to a mapped drive before running dos or a function that calls dos.

More About

expand all

Tips

  • To execute the operating system command in the background, include the trailing character, &, in the command argument (for example, 'notepad &'). 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.

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

See Also

| | | |

Was this topic helpful?