Passing python dictionary into matlab

Viewed 523

I need to pass python dictionary into matlab and matlab returns the variable after performing some operation as follows:

Matlab function in file named useMatlab4Sum.m:

function matlabSum = useMatlab4Sum(var)
    varin = struct(var)
    a = varin.var
    arr = varin.arr
    return a*arr(:)

In Python:

myDict = {'a':2, 'arr':np.array([[1, 2],[3, 4]])}
eng = matlab.engine.start_matlab()
params = eng.useMatlab4Sum(myDict)
eng.quit

But I get the error:

TypeError: unsupported Python data type: numpy.ndarray

I have a more complicated case that I am working through, I am just using the above simple case for example.

2 Answers

This problem is due to the diffrent handling of data type in python and matlab. you must change your code into this :

useMatlab4Sum(py.array.array(var))

more information on this link:

you need to pass the 'arr' as a list. use (arr.flatten()).tolist(), then in Matlab do this ClickHere

Related