{"id":1082,"date":"2016-08-03T13:23:33","date_gmt":"2016-08-03T13:23:33","guid":{"rendered":"https:\/\/block.arch.ethz.ch\/blog\/?p=1082"},"modified":"2016-08-03T14:14:17","modified_gmt":"2016-08-03T14:14:17","slug":"running-matlab-as-a-python-subprocess","status":"publish","type":"post","link":"https:\/\/block.arch.ethz.ch\/blog\/2016\/08\/running-matlab-as-a-python-subprocess\/","title":{"rendered":"Running Matlab as a Python subprocess"},"content":{"rendered":"<p>Although NumPy and Scipy usually provide all the tools you need (and more) for scientific computing, sometimes you may want to use a bit of Matlab in the background. In my case, for example, to use <code>rref<\/code>. Conveniently, Matlab can be run as a Python subprocess, provided of course that Matlab is installed on your system.<\/p>\n<p><!--more--><\/p>\n<pre class=\"brush:py;toolbars:false;gutter:false\">\r\nimport os\r\n\r\nfrom subprocess import Popen\r\nfrom scipy.io import loadmat\r\nfrom scipy.io import savemat\r\n\r\nimport numpy as np\r\n\r\ninfile = os.path.join(os.path.dirname(__file__), 'infile.mat')\r\noutfile = os.path.join(os.path.dirname(__file__), 'outfile.mat')\r\n\r\nwith open(infile, 'wb+') as fp: pass\r\nwith open(outfile, 'wb+') as fp: pass\r\n\r\nA = np.array([\r\n[1, 0, 0, 0],\r\n[0, 1, 0, 0],\r\n[0, 0, 1, 0],\r\n[0, 0, 0, 1]], dtype=float)\r\n\r\nindict = {'A': A}\r\n\r\nsavemat(infile, indict)\r\n\r\nmatlab  = ['matlab']\r\noptions = ['-nosplash', '-r']\r\ncommand = [\"load('{0}');[R, jb]=rref(A);save('{1}');exit;\".format(infile, outfile)]\r\n\r\n# on mac use the full path to matlab or add the path to your .profile\r\n# matlab = ['\/Applications\/MATLAB_R2016a.app\/bin\/matlab']\r\n\r\n# on windows use:\r\n# options = ['-nosplash', '-wait', '-r']\r\n\r\np = Popen(matlab + options + command)\r\n\r\nstdout, stderr = p.communicate()\r\n\r\noutdict = loadmat(outfile)\r\njb = outdict['jb'].tolist()[0]\r\n\r\nprint jb\r\n\r\n# should be [1, 2, 3, 4]\r\n# don't forget Matlab is 1-based\r\n# so convert to [0, 1, 2, 3]\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Although NumPy and Scipy usually provide all the tools you need (and more) for scientific computing, sometimes you may want to use a bit of Matlab in the background. In my case, for example, to use rref. Conveniently, Matlab can be run as a Python subprocess, provided of course that Matlab is installed on your [&hellip;]<\/p>\n","protected":false},"author":11,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[10],"tags":[],"class_list":["post-1082","post","type-post","status-publish","format-standard","hentry","category-code"],"_links":{"self":[{"href":"https:\/\/block.arch.ethz.ch\/blog\/wp-json\/wp\/v2\/posts\/1082","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/block.arch.ethz.ch\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/block.arch.ethz.ch\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/block.arch.ethz.ch\/blog\/wp-json\/wp\/v2\/users\/11"}],"replies":[{"embeddable":true,"href":"https:\/\/block.arch.ethz.ch\/blog\/wp-json\/wp\/v2\/comments?post=1082"}],"version-history":[{"count":5,"href":"https:\/\/block.arch.ethz.ch\/blog\/wp-json\/wp\/v2\/posts\/1082\/revisions"}],"predecessor-version":[{"id":1087,"href":"https:\/\/block.arch.ethz.ch\/blog\/wp-json\/wp\/v2\/posts\/1082\/revisions\/1087"}],"wp:attachment":[{"href":"https:\/\/block.arch.ethz.ch\/blog\/wp-json\/wp\/v2\/media?parent=1082"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/block.arch.ethz.ch\/blog\/wp-json\/wp\/v2\/categories?post=1082"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/block.arch.ethz.ch\/blog\/wp-json\/wp\/v2\/tags?post=1082"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}