{"id":378,"date":"2010-05-17T16:56:33","date_gmt":"2010-05-17T16:56:33","guid":{"rendered":"https:\/\/blogs.mathworks.com\/desktop\/2010\/05\/17\/calling-shell-commands-from-matlab\/"},"modified":"2010-05-17T16:56:33","modified_gmt":"2010-05-17T16:56:33","slug":"calling-shell-commands-from-matlab","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/community\/2010\/05\/17\/calling-shell-commands-from-matlab\/","title":{"rendered":"Calling Shell Commands from MATLAB"},"content":{"rendered":"<p>I&#8217;ve written before about <a href=\"https:\/\/blogs.mathworks.com\/community\/2009\/07\/06\/calling-java-from-matlab\/\">accessing Java from the Command Line<\/a>, 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:<\/p>\n<ul>\n<li><a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2010a\/techdoc\/ref\/system.html\"><tt>system<\/tt><\/a>: The <tt>system<\/tt> 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:\n<pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">s = system(<span style=\"color: #A020F0\">'dir'<\/span>)<\/pre>\n<pre style=\"font-style:oblique\"> Volume in drive H is mkatz \r\n Volume Serial Number is 0702-7BA0 \r\n \r\n Directory of h:\\www\\blog\\java \r\n \r\n05\/17\/2010  09:22 AM    &lt;DIR&gt;          . \r\n04\/19\/2010  08:27 AM    &lt;DIR&gt;          .. \r\n07\/02\/2009  01:10 PM                51 parsedouble.m \r\n05\/17\/2010  09:43 AM    &lt;DIR&gt;          wordpress_html \r\n07\/02\/2009  03:49 PM                85 inmemex.m \r\n07\/02\/2009  04:04 PM                31 tmp.m \r\n07\/08\/2009  08:02 AM               154 swing.m \r\n07\/10\/2009  09:39 AM               134 size_listen.m \r\n08\/17\/2009  01:43 PM               593 m3m.m \r\n04\/26\/2010  08:26 AM               146 heap_size.m \r\n04\/26\/2010  08:29 AM                44 maxmem.m \r\n05\/17\/2010  09:22 AM                17 system_commands.m \r\n               9 File(s)          1,255 bytes \r\n               3 Dir(s)   8,975,208,448 bytes free \r\n\r\ns =\r\n\r\n     0\r\n\r\n<\/pre>\n<p>The nice thing about the <tt>system<\/tt> function is that it is platform independent, which means if you don&#8217;t care where your code will run and the command it will issue is the same, you can reuse this function. Note that <tt>system<\/tt> 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 <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2010a\/techdoc\/ref\/isunix.html\"><tt>isunix<\/tt><\/a>, <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2010a\/techdoc\/ref\/ispc.html\"><tt>ispc<\/tt><\/a>, <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2010a\/techdoc\/ref\/ismac.html\"><tt>ismac<\/tt><\/a>, and <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2010a\/techdoc\/ref\/computer.html\"><tt>computer<\/tt><\/a> functions will allow you to programmatically determine what operating system the program is running on.\n<\/li>\n<li>There&#8217;s also the <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2010a\/techdoc\/matlab_env\/f0-12994.html#f0-38522\"><tt>!<\/tt>, or exclamation point<\/a> operator. This is symbol is sometimes also known as &#8220;bang&#8221; or &#8220;hey there&#8221;, and it&#8217;s just a shorthand for the above mentioned <tt>system<\/tt> operation. The &#8220;!&#8221; operation is quick an easy, but can&#8217;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 &#8220;!&#8221; at the start of the line, write out a normal MATLAB expression and then delete the &#8220;!&#8221; when you&#8217;re done.<\/li>\n<\/ul>\n<p>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 (&#8220;&#038;&#8221;) 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 <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2010a\/techdoc\/ref\/evalc.html\"><tt>evalc<\/tt><\/a> function:<\/p>\n<pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">s = evalc(<span style=\"color: #A020F0\">'system(''dir'')'<\/span>)<\/pre>\n<pre style=\"font-style:oblique\">\r\ns =\r\n\r\n Volume in drive H is mkatz \r\n Volume Serial Number is 0702-7BA0 \r\n \r\n Directory of h:\\www\\blog\\java \r\n \r\n05\/17\/2010  09:22 AM    &lt;DIR&gt;          . \r\n04\/19\/2010  08:27 AM    &lt;DIR&gt;          .. \r\n07\/02\/2009  01:10 PM                51 parsedouble.m \r\n05\/17\/2010  10:03 AM    &lt;DIR&gt;          wordpress_html \r\n07\/02\/2009  03:49 PM                85 inmemex.m \r\n07\/02\/2009  04:04 PM                31 tmp.m \r\n07\/08\/2009  08:02 AM               154 swing.m \r\n07\/10\/2009  09:39 AM               134 size_listen.m \r\n08\/17\/2009  01:43 PM               593 m3m.m \r\n04\/26\/2010  08:26 AM               146 heap_size.m \r\n04\/26\/2010  08:29 AM                44 maxmem.m \r\n05\/17\/2010  10:03 AM                28 system_commands.m \r\n               9 File(s)          1,266 bytes \r\n               3 Dir(s)   8,975,208,448 bytes free \r\n<\/pre>\n<p>There are also platform-specificthese system commands: <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2010a\/techdoc\/ref\/dos.html\"><tt>dos<\/tt><\/a> and <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2010a\/techdoc\/ref\/unix.html\"><tt>unix<\/tt><\/a>. However, these commands are exactly the same as <tt>system<\/tt>. If I had to guess, we&#8217;ve probably been keeping them around for compatibility reasons. <\/p>\n<p>My personal favorite of all of these is <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2010a\/techdoc\/ref\/winopen.html\"><tt>winopen<\/tt><\/a> which is Windows-only, and it&#8217;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:<\/p>\n<pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">winopen(pwd)<\/pre>\n<p>Matt Dunham at the <a href=\"http:\/\/www.advancedmcode.org\/calling-java-r-c-perl-system-commands.html\">Advanced MATLAB blog<\/a> 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. <\/p>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;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,&#8230; <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/community\/2010\/05\/17\/calling-shell-commands-from-matlab\/\">read more >><\/a><\/p>\n","protected":false},"author":38,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[41,45],"tags":[],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/community\/wp-json\/wp\/v2\/posts\/378"}],"collection":[{"href":"https:\/\/blogs.mathworks.com\/community\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.mathworks.com\/community\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/community\/wp-json\/wp\/v2\/users\/38"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/community\/wp-json\/wp\/v2\/comments?post=378"}],"version-history":[{"count":0,"href":"https:\/\/blogs.mathworks.com\/community\/wp-json\/wp\/v2\/posts\/378\/revisions"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/community\/wp-json\/wp\/v2\/media?parent=378"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/community\/wp-json\/wp\/v2\/categories?post=378"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/community\/wp-json\/wp\/v2\/tags?post=378"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}