File Exchange

image thumbnail

HelloMex

version 1.0.3 (7.34 KB) by Pascal Enderli
Minimal example MATLAB Mex wrapper for C++ classes

3 Downloads

Updated 15 Feb 2021

From GitHub

View Version History

View license on GitHub

****************************************************************************************************************
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 (2021). HelloMex (https://github.com/pascalenderli/HelloMex/releases/tag/v1.0.3), GitHub. Retrieved .

Comments and Ratings (5)

Pink_panther

I am using R2020B, latest update 4 to test, all good.

Pascal Enderli

Thanks for the feedback.

I guess everything is alright. You can not run Hello.m file just like a script, since it only declares and defines a MATLAB class.
You have to construct an object and call the objects Methods from the outside.

Example:
hello_onj = Hello(5)
obj.Compute(2) % -> returns 10 (2*5)

Notice:
Please use Github for issues if possible:
https://github.com/pascalenderli/HelloMex

Pink_panther

I downloaded update, and ran make_hello_mex.m, it said "started testing..", then suddenly MATLAB closed without any warning. Start MATLAB again, it shows a HelloMex.mexw64 created, so I ran Hello.m, here is the response.
>> run('C:\ml\Mex_Wrapper_CPP\Hello.m')
[Fail] [Line: 368] Expected an int as input.
Not enough input arguments.

Error in Hello (line 59)
this.handle_ = HelloMex("New", preset);

Error in run (line 91)
evalin('caller', strcat(script, ';'));

Pascal Enderli

Thanks for the feedback:
HelloMex (v1.1.2) now supports MinGw64 compiler.

Pink_panther

got error in 2020B,
MEX configured to use 'MinGW64 Compiler (C++)' for C++ language compilation.
Building with 'MinGW64 Compiler (C++)'.
Error using mex
C:\ml\Mex_wrapper_for_CPP\HelloMex.cpp:205:7: warning: direct base
'matlab::mex::Function' inaccessible in 'MexFunction' due to ambiguity
class MexFunction : public matlab::mex::Function, MatlabLogger
^~~~~~~~~~~

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!