Call Python, Java and C from your MATLAB code
Keep using the libraries you rely on. Call Python packages, Java classes and C libraries directly, and build your MEX files without MATLAB installed.
r = py.math.sqrt(81);A = reshape(1:6, 2, 3);s = py.numpy.sum(A);v = java.lang.Integer.parseInt("42");disp([r s v])
$ runmat interop.m9 21 42
Use the code you already have
Python packages
NumPy, SciPy, pandas and your own modules. Large arrays reach NumPy without being copied.
Java libraries
In-house JARs, database drivers and any class on your classpath.
C libraries
Point RunMat at a header and call the library's functions, with arrays passed by pointer.
MEX files
Rebuild your C, C++, Fortran and CUDA MEX source with runmat mex. No MATLAB needed.
Call Python packages
Use the py. prefix to call any module, class or function, and pyrun to run a few lines of Python with your variables. Numbers, strings, cells and structs convert both ways.
- NumPy reads RunMat arrays in place, so even a 3.2 GB matrix passes without a copy
- Python errors come back as RunMat errors with the Python traceback
- Pass a function handle where Python expects a callback
- Run Python in a separate process if you want a crash kept out of your session
A = rand(20000, 20000);n = py.numpy.linalg.norm(A);answer = pyrun("answer = x + 2", "answer", "x", 40);
Call Java classes
Create Java objects and call their methods with the same syntax MATLAB code uses. RunMat finds the JVM on your machine and starts it the first time you use Java.
- Overloaded methods are picked the way MATLAB picks them
- Java exceptions come back with the Java class, message and stack
- Pass a function handle where Java expects a listener
- List JARs in
runmat.tomlso every run of the project finds them
b = javaObject("java.lang.StringBuilder");javaMethod("append", b, "RunMat");disp(javaMethod("toString", b))v = java.lang.Integer.parseInt("42");m = javaMethod("max", "java.lang.Math", 4, 9);
Call C libraries
Give RunMat a shared library and its header. It reads the declarations and you call the functions with calllib or the clib. prefix.
- Run a library in RunMat's process to pass arrays by pointer with no copy, or in a separate process so a crash can't take down your session
runmat native-interface preparesaves the interface, so a project or compiled program uses the exact library it was built against- Pointers the library returns stay tied to it, so they can't be used after the library is unloaded
loadlibrary("libfilters.dylib", "filters.h", "alias", "filters");y = calllib("filters", "smooth", x, 5);% or, with a prepared interfacey = clib.filters.smooth(x, 5);
Build MEX files without MATLAB
runmat mex builds your existing MEX source with the compiler already on your machine. Everything else it needs is built into RunMat.
| C | C Matrix and MEX API |
|---|---|
| C++ | Modern C++ Data and Engine API |
| Fortran | Fortran Matrix and MEX API |
| CUDA | GPU MEX API, on Linux and Windows |
#include "mex.h"void mexFunction(int nlhs, mxArray *plhs[],int nrhs, const mxArray *prhs[]) {plhs[0] = mxDuplicateArray(prhs[0]);double *v = mxGetDoubles(plhs[0]);for (mwSize i = 0; i < mxGetNumberOfElements(plhs[0]); ++i)v[i] += 1.0;}
$ runmat mex --R2018a -o add_one add_one.cBuilt ./add_one.mexmaca64
y = add_one([1 2 3]) % 2 3 4
Use them in compiled programs and on clusters
List Python wheels, JARs, C libraries and MEX modules in runmat.toml. They're included in compiled executables and cluster jobs, so your program finds them wherever it runs.
FAQ
01Do I need MATLAB to build MEX files?
No. RunMat includes everything MEX builds need except a compiler. Use the C, C++ or Fortran compiler already on your machine, or nvcc for CUDA.
02Do my existing .mex files work?
Rebuild them from source with runmat mex. Binaries built for MATLAB need to be rebuilt to run in RunMat.
03Which Python and Java versions are supported?
CPython 3.9 to 3.13 and Java 17 to 21. Set the version in runmat.toml if more than one is installed.
04Are arrays copied when I call another language?
Not when Python, a C library or a MEX file runs in RunMat's process: they read RunMat's memory directly, and a copy is made only if they write to it. Java receives a copy.
05What happens if a library crashes?
Code running in a separate process stops with an error and your RunMat session keeps going. You can run Python, C libraries and MEX files that way.
06Does this work in the browser?
Python, Java, C and MEX calls run in the desktop app and the command line. The browser version runs everything else.