1

I need to run a matlab script from python. The matlab script returns an array, which I guess could be saved as a *.mat file.

Is it possible to execute the matlab code directly from python, and then retrieve it?

for example:

myarr = somematlabbridge.execute("matlab function")

Or otherwise, is it possible to do something like this:

somematlabbridge.execute("matlab functions to execute the matlab and save it in a file")

so that I can then read the file into Python to retrieve the output of the matlab function?

The output that I need from matlab will be a 2D array. By the way, I'm running Python from Anaconda/Spyder.

Edit

Let me clarify exactly what I'm trying to do. There's this software here that I want to execute, which is Matlab. I don't know really know Matlab. But the script works by simply running demo.m. When you run that, then the script creates a bunch of things, including an array called boxes. I know this because I can see the code, and because, while in the Matlab environment, I can click on the boxes variable in the Workspace and Matlab will show me its contents. Like in this image below: enter image description here

This 2D array called boxes is what I need to take into my Python script for processing.

So how can this be done? I don't really know Matlab so I'm not sure if I can say that the demo script is "returning the boxes array", really the last line in the script is only this:

boxes = BoxRemoveDuplicates(boxes);

So I added this line to Matlab code:

return boxes;

And then did this in Python:

import win32com.client
h = win32com.client.Dispatch('matlab.application')
myboxes= h.Execute ("PATH_TO_FILE\\demo.m")
print myboxes

Which gives me

??? Error: Unexpected MATLAB operator.

What's the correct way to do this?

user961627
  • 11,059
  • 35
  • 119
  • 202
  • what do you get, if you execute print (myarr) ? – Furtano May 21 '14 at 15:04
  • I've no time for a full answer right now, but you can call MATLAB from the command prompt (see: http://www.mathworks.com/matlabcentral/answers/102082-how-do-i-call-matlab-from-the-dos-prompt about half way down the answer) so I expect you can use `subprocess` to call MATLAB directly: http://stackoverflow.com/q/89228/2449192 You can have your MATLAB script write to a text file or some such that you can read in with `numpy` (or other file IO routines) – darthbith May 21 '14 at 15:09
  • @Furtano ideally I should just get a 2D array printed out. – user961627 May 21 '14 at 15:28

1 Answers1

1

I would say that return boxes is invalid syntax in MATLAB.

If you want the demo function to return boxes, you would need to make sure the first line of the functi0n looks like this:

function boxes = demo(yourInputArguments)

Not sure how to call it from python.

If things don't work out, you can also try to write the output of the matlab function to a text file, which you can than load in from any program. For that add the following line at the bottom of the file:

dlmwrite('boxes.txt',boxes)

Or, if you just want to see the result, consider adding only this word to the end of the function, basically a command to print the content of boxes to stdout:

boxes
jerad
  • 2,008
  • 14
  • 16
Dennis Jaheruddin
  • 19,745
  • 7
  • 58
  • 100