1

I am performing a matlab calculation through python. For this purpose, I use the following command:

retcode=subprocess.call["matlab","-nosplash","nodesktop","-wait","-r","run('matlabscript.m')","quit;"])

by running this command line in python, a matlab session opens and I can start to execute the aforementioned script 'matlabscript.m'. Is there a way to get the output of the execution of this script into python? retcode, does not contain anything unless a single number. I have tried to use subprocess.check_output instead, but I cannot get anything. Basically, when I execute the aforementioned sript a casename is going to be created and specified.

10
  • Have you tried subprocess.Popen with stdout=subprocess.PIPE and the communicate method? Commented Jun 17, 2015 at 14:53
  • @cdarke answers go in the answers box. Commented Jun 17, 2015 at 15:01
  • @AaronHall: it's not an answer because the question specifically requires subprocess.call, not Popen. Commented Jun 17, 2015 at 15:11
  • @cdarke read the source, same thing: hg.python.org/cpython/file/8667c26e2bec/Lib/subprocess.py#l552 Commented Jun 17, 2015 at 15:36
  • I think that communicate method is when you would like to insert the sequence of commands to specify when the matlab script runs. Are you certain about the usage of this command? Commented Jun 17, 2015 at 15:45

3 Answers 3

1

I suggest you use Popen. I don't have matlab, so I can't test your exact command, but try this:

import subprocess

cmd = ["matlab", "-nosplash", "no desktop", "-wait", "r", "run('matlabscript.m')","quit;"]

proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
(output, error) = proc.communicate()

if error:
    print "error:", error

print "output:", output

# if you need this:
retcode = proc.returncode

If a huge amount of output is produced then this could potentially crash, its a judgement only you can make.

Note that subprocess.communicate() is also used for stdin.

Sign up to request clarification or add additional context in comments.

2 Comments

I may need to clarify that the output that I need is whatever comes out or is typed into the cmd of matlab. It is this the information that I want to get returned into python. By the way, by following your method, the output I get is empty.
@DomenicodiCugno: I'm assuming your phrase "output" means stdout, the standard output stream. If it is sent somewhere else then you will have to capture it some other way. This could be why check_output does not work. Did you get anything through the error (stderr) stream? Can you confirm what you mean by output?
0

You could use subprocess.check_output which will wait for the result:

subprocess.check_output(["matlab","-nosplash","nodesktop","-wait","-r","run('matlabscript.m')","quit;"])

1 Comment

I agree that this is a step in the right direction. But apparently "I have tried to use subprocess.check_output instead, but I cannot get anything", so something else is going on.
0

If you're using Matlab >= R2014b, you can simply use the Matlab Engine for Python provided by Mathworks.

>>> import matlab.engine
>>> worker = matlab.engine.start_matlab()
>>> a = worker.eval('rand(10,10)')
>>> print(a)
[[0.8147236863931789,0.15761308167754828,0.6557406991565868,0.7060460880196088,0.43874435965639824,0.27602507699857837,0.7512670593056529,0.8407172559836625,0.35165950706299676,0.07585428956306361],[0.9057919370756192,0.9705927817606157,0.035711678574189554,0.031832846377420676,0.3815584570930084,0.6797026768536748,0.2550951154592691,0.25428217897153105,0.8308286278962909,0.05395011866660715],[0.12698681629350606,0.9571669482429456,0.8491293058687771,0.27692298496088996,0.7655167881490024,0.6550980039738407,0.5059570516651424,0.8142848260688164,0.5852640911527243,0.5307975530089727],[0.9133758561390194,0.4853756487228412,0.9339932477575505,0.04617139063115394,0.7951999011370632,0.16261173519463057,0.699076722656686,0.2435249687249893,0.5497236082911395,0.7791672301020112],[0.6323592462254095,0.8002804688888001,0.6787351548577735,0.09713178123584754,0.1868726045543786,0.11899768155837664,0.8909032525357985,0.9292636231872278,0.91719366382981,0.934010684229183],[0.09754040499940952,0.14188633862721534,0.7577401305783334,0.8234578283272926,0.48976439578823106,0.49836405198214295,0.9592914252054443,0.34998376598480874,0.28583901882037355,0.12990620847373013],[0.2784982188670484,0.421761282626275,0.7431324681249162,0.694828622975817,0.4455862007108995,0.9597439585160811,0.5472155299638031,0.19659525043120818,0.7572002291107213,0.5688236608721927],[0.5468815192049838,0.9157355251890671,0.39222701953416816,0.31709948006086053,0.6463130101112646,0.3403857266661332,0.13862444282867914,0.25108385797603106,0.7537290942784953,0.4693906410582058],[0.9575068354342976,0.7922073295595544,0.6554778901775566,0.9502220488383549,0.7093648308580726,0.5852677509797773,0.14929400555905747,0.6160446761466392,0.38044584697535666,0.011902069501241397],[0.9648885351992765,0.959492426392903,0.17118668781156177,0.03444608050290876,0.7546866819823609,0.22381193949113698,0.25750825412373646,0.47328884890272926,0.5678216407252211,0.3371226443988815]]

As an alternative, switch to GNU Octave and simple use Oct2Py.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.