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?

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

6 Comments

Thanks! How can I convert the matlab.double() arrays to python lists?
@sodiumnitrate matlab.double() should return a list of lists in Python (how Python will represent the array)
For instance, in the example shown in the docs, 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]].
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.
@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.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.