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 DOS command and return output


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



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


[status,cmdout] = dos(command) also 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.


[status,cmdout] = dos(command,'-echo') also 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.


collapse all

To create a folder named mynew, call a DOS command and save the exit status to a variable.

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


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

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

status = dos('notepad &')
status =


The status of zero indicates that Notepad successfully started.

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

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


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 call a valid DOS command, status indicates success and cmdout contains the command output.

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

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


results =

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

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

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
Name of second file to compare: collatz.asv
Option: /A
Comparing collatz.m and collatz.asv... 
Files compare OK 
Compare more files (Y/N) ? N

Input Arguments

collapse all

MS-DOS command, specified as a character vector. The command can be a Windows UI program that opens a 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 started MATLAB.

Example: 'dir'

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


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

Introduced before R2006a

Was this topic helpful?