Execute DOS command and return output
status = dos(command)
[status,cmdout] = dos(command)
[status,cmdout] = dos(command,'-echo')
To create a folder named
a DOS command and save the exit status to a variable.
command = 'mkdir mynew'; status = dos(command)
status = 0
status of zero indicates that the
was created successfully.
Open Microsoft® Notepad and immediately return the
exit status to MATLAB by appending an ampersand (
status = dos('notepad &')
status = 0
status of zero indicates that Notepad
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 call a valid DOS command,
cmdout contains the command output.
Attempt to execute a command called
Then, view the
[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 call an invalid DOS command,
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
[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 >>
command— MS-DOS command
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.
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. MATLAB returns this error:
Error using dos DOS commands may not be executed when the current directory is a UNC pathname
dosor a function that calls
To execute the operating system command in the background,
include the trailing character,
&, in the
'notepad &'). 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
cmdout is empty.