Calling MatLab from Python and Back
Khaled Khairy
khairy at magnet.fsu.edu
Mon Jun 18 14:56:32 EDT 2001
Hi everyone, I know some effort has been done before to interface Matlab
with Python ( specifically for the NumPy arrays). If this group here is
not the appropriate one for my problem, I'd appreciate you pointing me
to where you think I should go.
I have my own matrix classes that I would like to interface. I used the
MatLab provided "Engine.h" then I wrap my own API calls with BOOST, and
it works fine when I export matrices to MatLab, when I send calls using
engEval etc... (as long as I don't try to Plot anything).
The problem :
engGetArray crashes when the matrix is changed in the Matlab workspace.
Either directly or through an engEvalCall
Example:
----------- In Python -----------------------
>> x = api_rand(3,3) # this should return a 3x3 random matrix.
----------- In the C++ API -----------------
matrix api_rand_py1() // return a random matrix from
Matlab
{
Engine *ep = mat_open(); //get handle on the MatLab
session
engEvalString(ep,"InvM = rand(4,4)");
mxArray * MxHandle = engGetArray(ep,"InvM" ); <-----CRASHES HERE
matrix mx = mlab_to_gamma(MxHandle); //convert to my own class --
this works and has been tested
return mx ; //Return
the resulting matrix
}
________________________________________________________________
The confusing part is that this function works
matrix api_diag_py1(matrix& mx, std::string mx_str)
//retrieve the diagonal elements and put them into a vector
{
Engine *ep = mat_open(); //get handle
on the MatLab session
gamma_to_mlab(ep,mx, "G" ); //Convert from GAMMA format to
MATLAB format
engEvalString(ep,"G = diag(G);"); //Perform the actual call --
Note: aren't we changing the matrix here too
mxArray * MxHandle = engGetArray(ep,"InvM" ); <-----CRASHES
HERE
matrix mx = mlab_to_gamma(MxHandle); //convert to
my own class -- this works and has been tested
return mx ;
//Return the resulting matrix
}
------------------------------------------------
I tried to make this message short .... sorry ... I failed. I would
appreciate any feedbackthogh. Thank you all.
Khaled Khairy
NHMFL
Tallahassee, Fl
More information about the Python-list
mailing list