pymex - Matlab in Python in Matlab?

10 Mar 2012 17:08 Tags: matlab python

Original source page at


From original website:

pymex allows the programmer to use Python in Matlab.
pymex embeds a Python interpreter in Matlab, allowing Matlab programmers write parts of their scripts in Python. Programmers are also able to use Python modules in Matlab. pymex aims to be easy to use, simply pass it a string containing Python script and it will evaluate it. A special matlab module is provided to move data in between Matlab and Python (with restrictions).

I made some changes to make it work on Windows.

How to compile

1. Ensure that python is installed on the computer (2.7 is recommended).
2. Copy the pymex.cpp into the Matlab path(or work folder). Change 'c:/Python27/include/Python.h' in the 42 line of pymex.cpp, to your python header file.

#include <c:/Python27/include/Python.h>

3. Start Matlab, type
mex -setup

to setup the complier for mex and select Microsoft Visual C++ (I only test it under VC++)
4. Type
mex pymex.cpp -lpython27 -Lc:\Python27\libs
to compile the file.(change 'c:\Python27\libs' to your python libs location)
5. Try out the examples in matlab.
disp('make some variables...')
x = 2.3
y = rand(4, 3, 2)
z = 'durp durp durp'

disp('import the matlab module')
pymex('import matlab')

disp('"pull" the variables from matlab to python')
pymex('x, y, z = matlab.pull("x", "y", "z")')

disp('print the variables as seen by python')
pymex('matlab.mex_print(x, y, z)')

disp('"push" the variables from python to matlab')
pymex('matlab.push("x2", x, "y2", y, "z2", z)')

disp('test that y is equal to y2')
y == y2

How to use

pymex both embeds and extends Python. The embedding part is strait forward, the Python interpreter is embedded in Matlab. The extending part is were things get interesting. pymex provides a special matlab module which only available when running Python inside of Matlab. This module allows the programmer to move data back and forth between Matlab and Python representations. The matlab module also lets programmers print to Matlab's command window from Python.

in Matlab

function: pymex
input parameter: cell of strings with each string a python command, or just a string
output: 1 for success and 0 for fail

in python

A module named 'matlab' has been added to the python evironment. Use 'import matlab' to import.

import matlab

Three methods are available in this module:
1. matlab.mex_print(…)
Print all arguments to the Matlab command window. Works similarly to Python's normal print function.
matlab.mex_print('hello', x, y, z)

2. matlab.pull(name1, name2, …) => var1, var2, …
Import Matlab variables, move Matlab arrays to Python sequences. For each variable name given as an argument, return the value of the variable in Matlab with that name, in the same order. The Matlab variable has "caller" scope. Uses Matlab's evalin function.

3. matlab.push(name1, var1, name2, var2, …)
Export Matlab variables, move Python sequences to Matlab arrays. For each variable name and value pair, create a Matlab variable with that name and value. The Matlab variable has "caller" scope. Uses Matlab's assignin function.
matlab.push("x", x, "y", y", "z", z)



Comments: 1

