0

I'm trying to use MATLAB engine to call a MATLAB function in Python, but I'm having some problems. After manage to deal with NumPy arrays as input in the function, now I have some error from MATLAB:

MatlabExecutionError: Undefined function 'simple_test' for input arguments of type 'int64'.

My Python code is:

import numpy  as np
import matlab
import matlab.engine
eng =  matlab.engine.start_matlab()

eng.cd()

Nn = 30
x= 250*np.ones((1,Nn)) 
y= 100*np.ones((1,Nn)) 
z = 32
xx = matlab.double(x.tolist())
yy = matlab.double(y.tolist())
Output = eng.simple_test(xx,yy,z,nargout=4)
A = np.array(Output[0]).astype(float)
B = np.array(Output[1]).astype(float)
C = np.array(Output[2]).astype(float)
D = np.array(Output[3]).astype(float)

and the Matlab function is:

function [A,B,C,D] = simple_test(x,y,z)

A = 3*x+2*y;
B = x*ones(length(x),length(x));
C = ones(z);
D = x*y';
end

Is a very simple example but I'm not able to run it! I know the problem is in the z variable, because when I define z=32 the error is the one I mentioned, and when I change for z=32. the error changes to

MatlabExecutionError: Undefined function 'simple_test' for input arguments of type 'double'.

but I don't know how to define z.

Cris Luengo
  • 55,762
  • 10
  • 62
  • 120
JCV
  • 447
  • 1
  • 5
  • 15
  • 1
    Please take particular note of the minimal in [mcve]. Remove any code that doesn't cause the error to help work out exactly what is causing the error. – David Jun 16 '20 at 04:20
  • Where did you define your function? Is it in a place where MATLAB can find it? The error you get in both cases is the same: MATLAB doesn’t know a function of that name. – Cris Luengo Jun 17 '20 at 00:09
  • Your code works fine for me – Paolo Jun 17 '20 at 08:18
  • @Paolo really? it works for you??? for me is not working...did you did something different? – JCV Jun 17 '20 at 22:54
  • @CrisLuengo I put both scripts the python and the matlab in the same folder – JCV Jun 17 '20 at 22:55
  • I wonder what the default directory is for MATLAB when it starts up. The current Python directory might not be the working directory for MATLAB. Try changing the directory as your first action when you start up MATLAB. Also, stupid question but needs to be asked: did you write your `simple_test` function in a file called `simple_test.m`? That is the only way that MATLAB will recognize it. – Cris Luengo Jun 17 '20 at 22:58
  • @CrisLuengo I have just checked, both matlab and python are working with the same directory, and yes, I have saved the file with the same name of the function, it works for you too? – JCV Jun 17 '20 at 23:08
  • If you start MATLAB normally, outside of Python, can you then run the function? – Cris Luengo Jun 17 '20 at 23:10
  • Yes, I can run the function – JCV Jun 17 '20 at 23:13
  • @CrisLuengo I manage to run... I don't know why now it's running... Thank you – JCV Jun 17 '20 at 23:19

0 Answers0