0

I have a code in python but now I have to call a function developed for another person in Matlab. I would like to do that with matlab engine, but I have an error when passing the variables to the function, I got:

TypeError: unsupported Python data type: numpy.ndarray

This is a simple python code, for more or less what I wanna do:

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)) 
zz = matlab.double([[32]])
xx = matlab.double(x.tolist())
yy = matlab.double(y.tolist())
Output = eng.simple_test(xx,yy,zz,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 this is an example of the matlab function:

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

Does someone know how to effectively pass NumPy arrays as variables in matlab engine?

JCV
  • 447
  • 1
  • 5
  • 15
  • Please don’t post duplicate questions. Instead, answer the questions you get as comments under your question and engage with the people trying to help. – Cris Luengo Jun 17 '20 at 02:31

1 Answers1

3

Try:

xx = matlab.double(x.tolist())
yy = matlab.double(y.tolist())
zz = matlab.double([[32]])
out = eng.simple_test(xx, yy, zz, nargout=4)

https://www.mathworks.com/help/matlab/matlab_external/matlab-arrays-as-python-variables.html

  • nop...same error TypeError: unsupported Python data type: numpy.ndarray – JCV Jun 16 '20 at 22:17
  • but `xx` and `yy` are not ndarray's, are you sure you passed the correct variables? if your actual code is different from the one shown make sure to convert all your arrays in the same way – user13759065 Jun 16 '20 at 22:25
  • Sorry, I haven't changed when I called the function, now the error is different, is : MatlabExecutionError: Undefined function 'simple_test' for input arguments of type 'int64'. – JCV Jun 16 '20 at 22:30
  • try `z = 32.0` instead of `z = 32` (so that it's implicitly converted to double not int64) – user13759065 Jun 16 '20 at 22:43
  • I did that...and the error change for double, the problem is in z, but I don't know how to pass... MatlabExecutionError: Undefined function 'simple_test' for input arguments of type 'double'. – JCV Jun 16 '20 at 22:44
  • you never showed the actual code for `simple_test` so I can only guess :) Try `z = [32.0,]`, and then define `zz` as we did before for xx/yy – user13759065 Jun 16 '20 at 22:46
  • the simple_test is the function [A,B,C,D] = passing_variables(x,y,z), but in matlab I cant save the file with the same name of the function, so, the name of the file is simple_test and inside is the function I have above – JCV Jun 16 '20 at 22:49
  • Thank you for helping me...Still not working, now I have: AttributeError: 'list' object has no attribute 'tolist' – JCV Jun 16 '20 at 22:52
  • wait are you saying the defined function name does not match the M-file name? that obviously won't work – user13759065 Jun 16 '20 at 22:52
  • That is not the problem because I have just changed now, and still same error :( – JCV Jun 16 '20 at 22:56
  • oops, I meant it to define `z` as an ndarray so `z = np.array([[32.0]])` and then build `zz` as before. Or just directly `zz = matlab.double([[32]])` – user13759065 Jun 16 '20 at 23:00
  • Nop...MatlabExecutionError: Undefined function 'simple_test' for input arguments of type 'double', :( – JCV Jun 16 '20 at 23:08
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/216172/discussion-between-jeniffer-barreto-and-user13759065). – JCV Jun 17 '20 at 22:53
  • To convert Matlab engine outputs to numpy arrays use `np.asarray(output_array)` – seralouk Nov 01 '21 at 22:25