Skip to main content

MATLAB engine connector

Project description

Meg: a MATLAB engine connector in Python

PyPI - Wheel

Meg is a Python module to interface with the MATLAB engine, allowing to transfer data between Python and MATLAB, and to call MATLAB code from Python.

Assuming you can run matlab from the command line (i.e. the main MATLAB executable is in your PATH), using Meg is as simple as:

import meg
import numpy

with meg.Engine() as engine:
    data = numpy.empty((4,3))
    
    # Copy Python data to the engine
    engine["data"] = data
    
    # Execute MATLAB instructions
    engine("count = numel(data)")
    
    # Get data from MATLAB
    print(engine["count"])

Meg can be installed as any Python package: get the latest stable version from PyPi using pip (e.g. python3 -m pip install meg) or clone the source code and add it to your Python path.

Connecting to MATLAB

The main workhorse of Meg is the Engine object: it must be started before sending data between Python and MATLAB. An engine can be started (and automatically stopped) using the following syntax:

import meg

with meg.Engine() as engine:
    # Interact with MATLAB
    pass
# Once we reach this point, the engine has been automatically stopped.

Note that once an engine is stopped, all data stored inside which has not been transfered to Python is lost.

It is also possible to manually start and stop the engine:

import meg

# Create the engine, but do not start it
engine = meg.Engine()

engine.open()
# Interact with MATLAB
engine.close()

For more complicated environment (e.g. multiple versions of MATLAB, executable not in PATH, etc.), it is possible to specify the root directory of your target installation of MATLAB, and the explicit command to run when starting the engine:

import meg

# Target a specific installation of MATLAB
meg.setup("/opt/MATLAB/R2012b")

# Start the engine with a non-default command
with meg.Engine("/opt/MATLAB/R2012b/bin/matlab -nosplash") as engine:
    pass

Getting data to and from MATLAB

Data can be exchanged between Python and MATLAB using the Engine object: to store the content of the Python object name foo in the MATLAB object called bar, simply write engine["bar"] = foo. The reverse operation (storing the content of the MATLAB object called bar to a Python object called foo), write foo = engine["bar"].

Numbers (and number containers) are translated as-is between MATLAB and Python. From Python, list, tuples and numpy arrays will be converted to MATLAB numeric arrays; from MATLAB, numeric arrays will be converted either to Python scalars or to numpy arrays.

From Python, heterogeneous containers (lists, tuples or numpy arrays containing object of different types) will be converted to MATLAB cell arrays; from MATLAB, cell arrays will be converted to numpy arrays.

From Python, dictionaries and structured numpy arrays are converted to MATLAB struct arrays, and the same applies to the reverse conversion.

Calling MATLAB code

MATLAB statements are run by calling the engine object: assuming you have stored an object called x in MATLAB, computing the number of elements in it is done through engine("count = numel(x)"). Note that objects are not automatically exchanged between Python and MATLAB: they must be explicitely stored in the MATLAB engine before using them in MATLAB code.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

meg-1.0.1.tar.gz (12.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

meg-1.0.1-py3-none-any.whl (10.8 kB view details)

Uploaded Python 3

File details

Details for the file meg-1.0.1.tar.gz.

File metadata

  • Download URL: meg-1.0.1.tar.gz
  • Upload date:
  • Size: 12.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.1 importlib_metadata/4.10.0 pkginfo/1.8.2 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.9

File hashes

Hashes for meg-1.0.1.tar.gz
Algorithm Hash digest
SHA256 bd1d184e4625c70794eedc4c94ba52e54f6fd96a1ead117b894055a380c2bc91
MD5 54d62e18792e40bb0956ad9a01750a8a
BLAKE2b-256 dd3c4b7c69c3dcd291354109975b4ab666f0f9414742c9bdbbea9d07311949d9

See more details on using hashes here.

File details

Details for the file meg-1.0.1-py3-none-any.whl.

File metadata

  • Download URL: meg-1.0.1-py3-none-any.whl
  • Upload date:
  • Size: 10.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.1 importlib_metadata/4.10.0 pkginfo/1.8.2 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.9

File hashes

Hashes for meg-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 0afe9e8ed357bcf6760a85ddf701b594d27e257121144eccd4e6de77e3581c34
MD5 4eef6c1fc731c4c6a416f14f8546ab8f
BLAKE2b-256 a6a58214376e34708589d5741dbc169d46d84eb419224ce3be1cee379ce10d6e

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page