HelloMex

Minimal example MATLAB Mex wrapper for C++ classes
115 Downloads
Updated 15 Feb 2021

****************************************************************************************************************
HelloMex
****************************************************************************************************************

The repositiory contains a simple example for MATLAB mex-file generation of C++ classes.
The files are documented in detail directly in the code.

Because of the architecture of mex files, two wrappers are needed in order to achieve expected
object behaviour and syntax in MATLAB.

One wrapper is on the C++ side, it overrides the operator() of a class called 'MexFunction'.
This method is executed whenever the mex-file is called out of MATLAB. Within the MexFunction
class a list of Hello (the underlaying C++ class) objects is managed. Each list item
represents the state of a class instance in MATLAB. The MexFunction class acts as a simple
command interpreter which calls the right Hello Method of the respective MATLAB object and
casts the datatypes between MATLAB and C++ before and after the Hello method execution.

The second wrapper is a MATLAB class called 'Hello' to express the analogy to the C++ Hello
class. The MATLAB verson of the Hello class takes care, that the function calls to the mex-file
hold the correct syntax and passes an object specific handle (identifier), such that the C++
MexFunction class always knows which object state it needs to refer to.

Github: https://github.com/pascalenderli/HelloMex
Matlab: https://www.mathworks.com/matlabcentral/fileexchange/87292-hellomex

===========================================================================
Bug Report
===========================================================================
Please use Github for Bug report:
https://github.com/pascalenderli/HelloMex/issues

===========================================================================
Getting Started
===========================================================================
* Run the make_hello_mex.m script to compile the mex file.
* Use `doc Hello` after you compiled the mex-file to get a documentation of the MATLAB class.
* Study the detailed comments inside the files.
* Enjoy and like on Github and File Exchange :-)

===========================================================================
Repository Files:
===========================================================================
This repository contains the following files.

-----------------------------------------------------------------------------------------------------------------------------------
hello.h / hello.cpp
-----------------------------------------------------------------------------------------------------------------------------------
Underlaying class which finally will be accessed from MATLAB. This is the actual
C++ class of which we want to use the functionality in MATLAB.

The class is initialized with a number called preset.
The value of preset can be requested using the method GetPreset() during the lifetime of the
object. Using the Method Compute(factor) the preset number is multiplied with the function
argument called factor.

-----------------------------------------------------------------------------------------------------------------------------------
HelloMex.cpp
-----------------------------------------------------------------------------------------------------------------------------------
Holds the implementation of the C++ Wrapper class 'MexFunction'. On the upper part of the
file is also a compile time configuration concerning the log level of the mex file.
This file translates to the mex-file after compilation.

-----------------------------------------------------------------------------------------------------------------------------------
Hello.m
-----------------------------------------------------------------------------------------------------------------------------------
The MATLAB wrapper for the mex file. This class manages calls to the mex-file and provides
a userfriendly class interface to interact with the underlined cpp class methods.

-----------------------------------------------------------------------------------------------------------------------------------
make_hello_mex.m
-----------------------------------------------------------------------------------------------------------------------------------
Script to compile the mex file and rudimentary testing of the code.

Cite As

Pascal Enderli (2024). HelloMex (https://github.com/pascalenderli/HelloMex/releases/tag/v1.0.3), GitHub. Retrieved .

MATLAB Release Compatibility
Created with R2019b
Compatible with any release
Platform Compatibility
Windows macOS Linux

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!
Version Published Release Notes
1.0.3

See release notes for this release on GitHub: https://github.com/pascalenderli/HelloMex/releases/tag/v1.0.3

1.0.2

See release notes for this release on GitHub: https://github.com/pascalenderli/HelloMex/releases/tag/v1.0.2

1.0.1

See release notes for this release on GitHub: https://github.com/pascalenderli/HelloMex/releases/tag/v1.0.1

To view or report issues in this GitHub add-on, visit the GitHub Repository.
To view or report issues in this GitHub add-on, visit the GitHub Repository.