0

I am using the matlab engine for python. My goal is to pass some info to my matlab script from python, and then store the result. I was able to do this if I created a matlab array of all the values I wanted, but I would really like a dictionary (so I can remember what value goes with what variable or if I change my matlab script in the future). This is what I've tried:

MATLAB function:

function out = mymatlabfunc(x,y)
    # compute stuff
    out = py.dict('interesting variable 1', x_out, 'interesting variable 2', y_out, ...);

and in python:

eng = matlab.engine.start_matlab()
xdata, ydata = matlab.double(x), matlab.double(y)
resultdict = eng.mymatlabfunc(xdata,ydata)

Unfortunately this doesn't return my dictionary, but a matlab.object that I don't know what to do with. Is it possible to return my dictionary, or should I simply return the numbers from matlab and make my dictionary in python?

qRTPCR
  • 1,656
  • 1
  • 13
  • 13
  • From m-side it's just a "Python Object" because matlab does not support dictionaries. Just call methods like `get` on it to get whantever you want. – Daniel Jun 21 '15 at 08:33
  • 1
    @Daniel I get an error saying that matlab.object has no attribute 'get'. Python sees this as a matlab object still, not a dictionary. – qRTPCR Jun 21 '15 at 17:33

2 Answers2

0

You could do a dir (resultdir) in order to know what methods are available.

Cristea Bogdan
  • 116
  • 1
  • 11
  • The only method I see available is size and stuff like '__class__' and '__getattribute__'. I can get the length of it (length 1), but it's a matlab object as far as python is concerned so I can't do normal dictionary operations with it. – qRTPCR Jun 21 '15 at 17:36
0

I figured it out from the mathworks website. Simply make a matlab structure for the return value of your script and python will see it as a dictionary. So my matlab code is now:

function out = mymatlabfunc(x,y)
    # compute stuff
    out = struct('interesting variable 1', x_out, 'interesting variable 2', y_out, ...);
qRTPCR
  • 1,656
  • 1
  • 13
  • 13