Hello !

I'm trying to do a similar thing as http://www.daniweb.com/software-development/python/threads/392697/calling-matlab.m-file-from-python.
I managed to have Matlab launch, and run my Matlab file as the GUI of my matlab function is shown on screen. However it only stays on screen for 1 second, and Matlab closes !

import subprocess as sp

command = """/Applications/MATLAB_R2012b.app/bin/matlab  -r "cd(fullfile('/Users/Jules/Dropbox/CODES/Matlab/')), coverGUI_AL_FOV" """

sh=sp.Popen(command, stdin=sp.PIPE, stdout=sp.PIPE, stderr=sp.PIPE, shell=True)

print sh.communicate()

By the way, I looked at http://docs.python.org/2/library/subprocess.html but I'm not getting what all the stdin/out/err are, so I just put these in my code with no real reason..

If anyone has an idea why the process seems to quit, that would help me a lot !
Thanks

Recommended Answers

All 12 Replies

The first thing to check is what happens when you run the command

/Applications/MATLAB_R2012b.app/bin/matlab  -r "cd(fullfile('/Users/Jules/Dropbox/CODES/Matlab/')), coverGUI_AL_FOV"

in an ordinary terminal (without python).

Well it works properly !
I noticed that closing the Terminal window closes the Matlab.
I'm guessing that it's the python script that ends, closing everything.

So I figured out something. I added .wait() at the end, and it seems to work.
sp.Popen(command, stdin=sp.PIPE, stdout=sp.PIPE, stderr=sp.PIPE, shell=True).wait()
In fact it may due to some other problem. In my script all the "os.system(...)" are run at launch without respecting things that should activate them like 'button of the tkinter gui is pressed'.
For example in this case

sh=sp.Popen(command, stdin=sp.PIPE, stdout=sp.PIPE, stderr=sp.PIPE, shell=True).wait()
print sh.communicate()

using sh=... is not interesting, as it immediately executes sh even if I delete print sh.communicate

You can remove the stdin argument as you don't send data to the subprocess. The communicate() method
should block until the matlab program returns, and it should return the output string and error string
of this process. What gets printed when you write

sh=sp.Popen(command, stdout=sp.PIPE, stderr=sp.PIPE, shell=True)

print sh.communicate()
print "return code was %d" % sh.returncode

?

Well it crashes like before the Matlab program window.
By adding .wait() I actually get this before it crashes :

Traceback (most recent call last):
  File "/Users/Jules/Dropbox/CODES/V1.6/Input_python2matlab.py", line 18, in <module>
    print sh.communicate()
AttributeError: 'int' object has no attribute 'communicate'
[Finished in 9.5s with exit code 1]

Try

sh=sp.Popen(command, stdout=sp.PIPE, stderr=sp.PIPE, shell=True)
sh.wait()
print sh.communicate()
print "return code was %d" % sh.returncode

Another possibility is that the matlab command returns immediately and starts another process which is the actual matlab. When you launch the command in a terminal, can you use the terminal while matlab is running ? (try the ls command for example)

Okay thanks Gribouillis ! Now it works without this message poping up. But what is the meaning of the line print "return code was %d" % sh.returncode ? ^^

By the way, with the .wait(), it waits for Matlab app to quit, and not just the program interface to be closed.
So if I want to use my Matlab analysis several times in my python script (apply it to different scenarios) I have to launch a Matlab process everytime (just discovered that you can weirdly have the Matlab called multiple times [a Matlab app icon pops in the Dock]) ?
Is there not a way for Matlab to 'give back the hand' to python without quitting the app ? / or the other way, python to kick back in when the Matlab window is closed ?
Thanks !

Please remove the wait() and post what gets printed ! A return code of 0 means a successful command.

Nope, the terminal is unusable when the matlab is running.

Well without the wait(), I get a return code of 0, but the Matlab interface quits after few seconds.
With the wait() I also get a 0 once the Matlab is done.

Let us try without shell, try

import shlex
args = shlex.split(command)
print args
sh=sp.Popen(args, stdout=sp.PIPE, stderr=sp.PIPE, shell=False)
print sh.communicate()
print "return code was %d" % sh.returncode

what happens, and what is the output ?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.