Skip to content

hanjinliu/matcall

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

55 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

matcall

This Python module makes it much easier to run MATLAB functions and use MATLAB classes in Python codes. You can do almost anything in Python as in MATLAB.

Contents

Following included:

  • MatCaller, which handles MATLAB.
  • MatFunction, which dynamically defines a function in Python from MATLAB function (including constructor).
  • MatClass, which dynamically defines a class in Python from MATLAB class.
  • MatStruct, which makes MATLAB's struct like object.
  • %%matlab magic command.

Preparation

First launch MATLAB in Python and make an instance by mat = MatCaller(). Make sure you have downloaded MATLAB engine API for Python correctly and added its path by such as sys.path.append.

from matcall import MatCaller
import numpy as np

mat = MatCaller()

To add paths to MATLAB engine, use addpath function. You can also recursively add directory paths that .m files are contained.

mat.addpath("path/to/the/file")
mat.addpath("path/to/the/file", recursive=True)

MATLAB to Python conversion table

MATLAB Python
logical bool
matrix (1x1) int or float
matrix (1xN) 1D ndarray
matrix (Mx1) 1D ndarray
matrix (MxN) ndarray
char str
cell list
struct MatStruct
function_handle MatFunction
table DataFrame
others MatClass

Python to MATLAB conversion table

Python MATLAB
bool logical
int or float matrix (1x1)
str or Path char
list or tuple cell
dict or MatStruct struct
ndarray matrix
MatFunction function_handle
DataFrame table
MatClass corresponding object

Basic Usage

Run MATLAB as if in MATLAB console.

%%matlab
data.time = 1:100;
data.signal = sin(data.time/5.2);
data.name = "wave";
data
data = 

struct with fields:

    time: [1x100 double]
    signal: [1x100 double]
    name: "wave"

MATLAB workspace is accessible via MatCaller object. MATLAB objects are automatically converted to Python objects:

mat.data.name
'wave'

or vice versa:

mat.x = 10
%matlab x
x =

  int64

   10

Use MATLAB Functions

MATLAB functions can be translated to Python function by

mMax = mat.translate("max")
mMax
MatFunction<max>
mMax(np.array([3,6,4]))
[Out]
    [6, 2.0]

MATLAB lambda function is also supported.

sq = mat.translate("@(t)t^2")
sq 
MatFunction<@(t)t^2>
sq(10)
100

Use MATLAB Classes, Properties and Methods

Translation of MATLAB class constructor is also possible. Here constructor (not the class itself!) is returned and Python class will be dynamically defined with it. Same object is sent to MATLAB workspace only when it's needed.

mycls = mat.translation("MyClass")
obj = mycls(x1, ..., xn)

Setter and getter are also (mostly) defined so that you can deal with the properties in a very simple way.

mplot = mat.translate("plot")
pl = mplot(x, y)    # A figure window is openned here.
pl.Color = "red"    # The line color is changed to red here.

Examples

Solve ODE using MATLAB ode45 function.

xlim = np.array([0., 20.])
v0 = np.array([2.,0.]).T
vdp1 = mat.translate("vdp1")
ode45 = mat.translate("ode45")
result = ode45(vdp1, xlim, v0)
result
MatStruct with 6 fields:
    solver: ode45
    extdata: MatStruct object (3 fields)
        x: np.ndarray (60,)
        y: np.ndarray (2, 60)
    stats: MatStruct object (3 fields)
    idata: MatStruct object (2 fields)

Calculate derivative using symbolic variables.

sym = mat.translate("sym")
diff = mat.translate("diff")
x = sym("x")
A = sym("A")
f = A * np.sin(x)**2
print(diff(f))
2*A*cos(x)*sin(x)