MATLAB Community

MATLAB, community & more

Calling Shell Commands from MATLAB

I’ve written before about accessing Java from the Command Line, but you can also make calls into other systems from MATLAB. For example, you can access libraries written in C/C++ or Fortran, connect to .NET processes (on Windows), and make calls to a system shell. Today I will discuss the different ways you can run a system shell command:

  • system: The system function will execute a command in your OS shell and return the execution status (generally 0 for success and 1 for an error). For example:
    s = system('dir')
     Volume in drive H is mkatz 
     Volume Serial Number is 0702-7BA0 
     
     Directory of h:\www\blog\java 
     
    05/17/2010  09:22 AM    <DIR>          . 
    04/19/2010  08:27 AM    <DIR>          .. 
    07/02/2009  01:10 PM                51 parsedouble.m 
    05/17/2010  09:43 AM    <DIR>          wordpress_html 
    07/02/2009  03:49 PM                85 inmemex.m 
    07/02/2009  04:04 PM                31 tmp.m 
    07/08/2009  08:02 AM               154 swing.m 
    07/10/2009  09:39 AM               134 size_listen.m 
    08/17/2009  01:43 PM               593 m3m.m 
    04/26/2010  08:26 AM               146 heap_size.m 
    04/26/2010  08:29 AM                44 maxmem.m 
    05/17/2010  09:22 AM                17 system_commands.m 
                   9 File(s)          1,255 bytes 
                   3 Dir(s)   8,975,208,448 bytes free 
    
    s =
    
         0
    
    

    The nice thing about the system function is that it is platform independent, which means if you don’t care where your code will run and the command it will issue is the same, you can reuse this function. Note that system is platform-independent, but the system commands you run using this are likely to be very dependent on your OS. If you have platform-specific code, the isunix, ispc, ismac, and computer functions will allow you to programmatically determine what operating system the program is running on.

  • There’s also the !, or exclamation point operator. This is symbol is sometimes also known as “bang” or “hey there”, and it’s just a shorthand for the above mentioned system operation. The “!” operation is quick an easy, but can’t be used to take functional arguments. Because we Tab-complete filenames with system operations, a dirty trick to complete file paths from the command line is to stick a “!” at the start of the line, write out a normal MATLAB expression and then delete the “!” when you’re done.

With either of these functions, there are a few things to keep in mind. By default they will pause the MATLAB execution until the system command exits. If you just want to kick off a separate system process and have MATLAB continue, append an ampersand (“&”) to the end of the command. Also, if you want to capture the output of a system command as a string, wrap the call as an input to the evalc function:

s = evalc('system(''dir'')')
s =

 Volume in drive H is mkatz 
 Volume Serial Number is 0702-7BA0 
 
 Directory of h:\www\blog\java 
 
05/17/2010  09:22 AM    <DIR>          . 
04/19/2010  08:27 AM    <DIR>          .. 
07/02/2009  01:10 PM                51 parsedouble.m 
05/17/2010  10:03 AM    <DIR>          wordpress_html 
07/02/2009  03:49 PM                85 inmemex.m 
07/02/2009  04:04 PM                31 tmp.m 
07/08/2009  08:02 AM               154 swing.m 
07/10/2009  09:39 AM               134 size_listen.m 
08/17/2009  01:43 PM               593 m3m.m 
04/26/2010  08:26 AM               146 heap_size.m 
04/26/2010  08:29 AM                44 maxmem.m 
05/17/2010  10:03 AM                28 system_commands.m 
               9 File(s)          1,266 bytes 
               3 Dir(s)   8,975,208,448 bytes free 

There are also platform-specificthese system commands: dos and unix. However, these commands are exactly the same as system. If I had to guess, we’ve probably been keeping them around for compatibility reasons.

My personal favorite of all of these is winopen which is Windows-only, and it’s basically what you would get if you typed the input command in the Run box of the Start menu. I use this command all the time to open a path in an explorer window, like so:

winopen(pwd)

Matt Dunham at the Advanced MATLAB blog wrote a nice post awhile back about a whole range of systems that you can access from the MATLAB commands, and expands on some of the concepts presented here.

|
  • print

댓글

댓글을 남기려면 링크 를 클릭하여 MathWorks 계정에 로그인하거나 계정을 새로 만드십시오.