Execute DOS command and return output
Issue a DOS command to create a new folder named
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 Notepad and immediately return the exit status to MATLAB by
appending an ampersand (
&) to the
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 issue 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 issue 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 >>
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.
status— Command exit status0 | nonzero integer
Command exit status, returned as either
a nonzero integer. When the command is successful,
status is a nonzero integer.
command includes the ampersand
the exit status upon command launch.
command does not include the
ampersand character (
the exit status upon command completion.
Output of the operating system command, returned as a string.
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.