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