1

I have a python code within which I want to manipulate a list using a Matlab function and return it as a new list to python.

To test matlab.engine, I've tried the following:

import matlab.engine
eng = matlab.engine.start_matlab()
eng.cd('~/Documents/someDirWithMatlabFunctions/')
a = testFnc(2)

where testFnc.m looks like

function [list2] = testFnc(list)
for i = 1:numel(list)
    list(i) = 3*list(i)
end
list2 = list;
end

When I run the python code, I get the following output:

>>> a = eng.testFnc(4)
>>> a
12L
>>> print a
12

My first question is what is 12L? Furthermore, when I try to pass a list as an argument:

>>> a = eng.testFnc([1,2,3])
Undefined function 'mtimes' for input arguments of type 'cell'.

It then references the line of the Matlab function in which the multiplication takes place, as where the error occurs. I had anticipated that this might be a problem, as lists and matrices are different things. How can I properly pass variables to and from Matlab?

sodiumnitrate
  • 2,899
  • 6
  • 30
  • 49

1 Answers1

3

What is 12L?

Python supports arbitrary precision integers, meaning you're able to represent larger numbers than a normal 32- or 64-bit integer type. The L tells you when a literal is of this type and not a regular integer.

Note, that L only shows up in the interpreter output, it's just signifying the type. That's why it doesn't show up when you print it.

How can I properly pass variables to and from Matlab?

Straight from MathWorks documentation:

The matlab Python package provides array classes to represent arrays of MATLAB numeric types as Python variables so that MATLAB arrays can be passed between Python and MATLAB.

The documentation goes on to give lots of helpful examples for how to pass variables from MATLAB.

To pass data back to MATLAB, I recommend using numpy/scipy. This answer explains more about how to do that.

Community
  • 1
  • 1
Bob Dylan
  • 1,773
  • 2
  • 16
  • 27
  • Thanks! How can I convert the `matlab.double()` arrays to python lists? – sodiumnitrate Aug 18 '15 at 14:34
  • @sodiumnitrate `matlab.double()` should return a list of lists in Python (how Python will represent the array) – Bob Dylan Aug 18 '15 at 14:39
  • For instance, [in the example shown in the docs](http://www.mathworks.com/help/matlab/matlab_external/matlab-arrays-as-python-variables.html#buglzvt), a 2-by-5 array is created and printed: `a = matlab.double([[1,2,3,4,5], [6,7,8,9,10]])`. Printing a (`print(a)`) results in a list of lists: `[[1.0,2.0,3.0,4.0,5.0],[6.0,7.0,8.0,9.0,10.0]]`. – Bob Dylan Aug 18 '15 at 14:40
  • I see that it is still a special type (`matlab.double`), but it can be accessed the same as Python lists. To turn it into Python lists you could just call `list()` on each list within the array. – Bob Dylan Aug 18 '15 at 14:42
  • Ok, so there is no need to 'convert' it to anything, I can just manipulate elements as though in a python list. – sodiumnitrate Aug 18 '15 at 14:42
  • 1
    @sodiumnitrate bingo. You can access the elements the same as you would if it were a default Python structure (list of lists), so why bother changing it? Plus not changing it probably helps with feeding it back into MATLAB. – Bob Dylan Aug 18 '15 at 14:45