Code covered by the BSD License  

Highlights from
Example MATLAB class wrapper for a C++ class

4.8

4.8 | 5 ratings Rate this file 90 Downloads (last 30 days) File Size: 3 KB File ID: #38964

Example MATLAB class wrapper for a C++ class

by Oliver Woodford

 

09 Nov 2012 (Updated 14 Jan 2013)

An example of how to safely encapsulate a C++ class in a MATLAB class, via a mex interface.

| Watch this File

File Information
Description

This package provides an example of how to wrap a C++ class in a MATLAB class, via a mex interface, safely, without memory leaks, whilst achieving an interface in MATLAB that is similar to the underlying C++ interface.

After downloading, first mex the C++ wrapper using:
>> mex class_interface_mex.cpp

Then test the various methods:
>> a = class_interface(); % Create a class instance
>> train(a) % Train the class - dummy method
>> test(a) % Test the class - dummy method
>> clear a; % Destroy the instance

Look in class_interface.m and class_interface_mex.cpp to see how this is implemented, and copy the structure to interface with your own C++ classes.

This submission is inspired by the following newsgroup thread:
http://www.mathworks.com/matlabcentral/newsreader/view_thread/278243
Thanks to all those who have contributed.

Acknowledgements

This file inspired Ray Casting For Deformable Triangular 3 D Meshes.

MATLAB release MATLAB 8.0 (R2012b)
Other requirements C++ compiler
Tags for This File  
Everyone's Tags
c, class, example, hello, mex, wrapper
Tags I've Applied
Add New Tags Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (14)
10 Mar 2013 Oliver Woodford

Szigeti: your compile error is not related to this submission.

Fernando: the error you get seems self explanatory. You need to make sure class_handle.hpp, which comes with this submission, is in the same folder as the cpp file being compiled.

10 Mar 2013 Szigeti

Hello
I have run the example successfully, and even managed to make new functions, in my class, and gave a matrix as an out parameter. However I want to import my code into the given class, as a function. It opens a dll in C++ to a COM 2 device. I have overloaded some operator like [], =, *; I have read that matlab support overloading, but this is not matlab code. So I am confused a bit. In my opinion the vs 2010 compiler that I have mex -setup-ed, should work. But it gives me this error. class_interface_mex.obj : error LNK2019: unresolved external symbol "public: class std::vector<int,class std::allocator<int> > & __cdecl EEGContainer::operator[](int)" (??AEEGContainer@@QEAAAEAV?$vector@HV?$allocator@H@std@@@std@@H@Z) referenced in function "public: double * * __cdecl dummy::getEeg(void)" (?getEeg@dummy@@QEAAPEAPEANXZ)
class_interface_mex.obj : error LNK2019: unresolved external symbol "public: int __cdecl EEGContainer::getDataLenght(void)" (?getDataLenght@EEGContainer@@QEAAHXZ) referenced in function "public: double * * __cdecl dummy::getEeg(void)" (?getEeg@dummy@@QEAAPEAPEANXZ)
class_interface_mex.obj : error LNK2019: unresolved external symbol "public: __cdecl EEGContainer::~EEGContainer(void)" (??1EEGContainer@@QEAA@XZ) referenced in function "public: double * * __cdecl dummy::getEeg(void)" (?getEeg@dummy@@QEAAPEAPEANXZ)
class_interface_mex.obj : error LNK2019: unresolved external symbol "public: class EEGContainer & __cdecl EEGContainer::operator=(class EEGContainer const &)" (??4EEGContainer@@QEAAAEAV0@AEBV0@@Z) referenced in function "public: double * * __cdecl dummy::getEeg(void)" (?getEeg@dummy@@QEAAPEAPEANXZ)
class_interface_mex.obj : error LNK2019: unresolved external symbol "public: __cdecl EEGContainer::EEGContainer(int)" (??0EEGContainer@@QEAA@H@Z) referenced in function "public: double * * __cdecl dummy::getEeg(void)" (?getEeg@dummy@@QEAAPEAPEANXZ)
class_interface_mex.obj : error LNK2019: unresolved external symbol "public: __cdecl BMRecorder::BMRecorder(void)" (??0BMRecorder@@QEAA@XZ) referenced in function "public: double * * __cdecl dummy::getEeg(void)" (?getEeg@dummy@@QEAAPEAPEANXZ)
class_interface_mex.mexw64 : fatal error LNK1120: 6 unresolved externals

31 Jan 2013 Fernando

hello everyone, I am having trouble when mexing the class_interface_mex.cpp in matlab. The error that matlab gives me is the follwing:

>> mex class_interface_mex.cpp
class_interface_mex.cpp
\\aus.aero-ad.tamu.edu\UgradUsers$\f0p1107\Home\MATLAB\class_interface_mex.cpp(2) : fatal error C1083: Cannot open include file: 'class_handle.hpp': No such file or directory

C:\PROGRA~1\MATLAB\R2012A\BIN\MEX.PL: Error: Compile of '\\aus.aero-ad.tamu.edu\UgradUsers$\f0p1107\Home\MATLAB\class_interface_mex.cpp' failed.

Error using mex (line 206)
Unable to complete successfully.

Could anyone help me by letting me know how I can get rid of this error? I am no expert at coding so please try to keep your answer as simple as possible. Any help will be much appreciated.

Thank you!

12 Jan 2013 Richard Crozier

Hmmm, the FEX comment box gobbled my libraries, it's the following two that I needed

cstring
typeinfo

12 Jan 2013 Richard Crozier

I had to include both of the following to get it to compile on Linux

#include <cstring>
#include <typeinfo>

I'm using gcc 4.4.6. It ordered me to use typeinfo, I needed cstring to get strcmp.

Thanks for this though, it's incredibly useful to me!

02 Jan 2013 Gabri Gwala

Thank you Andreas for you suggestion about compiling in Unix!!!

18 Dec 2012 Artem

That's it, Oliver! Thanks a lot, a wonderful contribution!

18 Dec 2012 Oliver Woodford

Artem: If you want to put this in a class directory then the mex file needs to go in a subdirectory of that folder called private.

18 Dec 2012 Artem

Oliver, It is just 'mex class_interface_mex.cpp', right? It works fine if the mex-file is in a non-@ directory, but in the class dir it fails.

17 Dec 2012 Oliver Woodford

Artem: Did you mex the file, as explained in the file description?

17 Dec 2012 Artem

Great contribution!
I wonder if it is possible to put the class in @class_interface directory? In my case, Matlab cannot find class_interface_mex() function. Any suggestions?

27 Nov 2012 Andreas

code needs some tweaking to compile on non-windows systems. What's the reason for using 'raw_name()' instead of 'name()'. The former is MS specific.

--- orig/class_handle.hpp 2012-11-08 15:34:08.000000000 +0100
+++ new/class_handle.hpp 2012-11-27 16:46:50.831263148 +0100
@@ -4,12 +4,28 @@
#include <stdint.h>
#include <string>

+#if !(defined _WIN32 || defined _WIN64) // needed to compile on linux.
+ #include <string.h>
+ #include <typeinfo>
+ #define raw_name name // it would be better to change the code so that we use 'name()' instead of 'raw_name()'
+#endif
+


--- orig/class_interface_mex.cpp 2012-11-08 16:10:26.000000000 +0100
+++ new/class_interface_mex.cpp 2012-11-27 16:46:47.375263270 +0100
@@ -1,6 +1,10 @@
#include "mex.h"
#include "class_handle.hpp"

+#if !(defined _WIN32 || defined _WIN64) // needed to compile on linux.
+ #include <string.h>
+#endif
+
// The class that we are interfacing to
class dummy
{

09 Nov 2012 Francesco Montorsi

Works great! perfect to start interfacing C++ object-oriented code to MATLAB

09 Nov 2012 Matthew

Great submission developed from an informative newsgroup thread (referred to in description).

Updates
12 Dec 2012

Fix compile error in Linux - thanks to Andreas for highlighting the issue.

02 Jan 2013

2nd attempt to fix compilation under linux.

14 Jan 2013

Add #includes suggested by Richard Crozier (thanks) to fix compilation in Linux.

Contact us