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:
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?