API to transfer references of C-classes to Matlab and back

Version 1.0.0.0 (3.51 KB) by Quant Guy
Interface to provide a way to transfer custom C/C++ class instance references to Matlab and to Mex
374 Downloads
Updated 15 Oct 2010

View License

matlab_c_ptr interface provides tools to cast pointers of any type to Matlab mxArray which then can be used to store a reference in Matlab workspace to class instances that later on can be accessed from the same mex-file or even from different mex-files. This avoids the pain to transfer especially HUGE "application logic classes" to Matlab and then back again to Mex through using Matlab structs of other silly methods. The interace is very low latency and thus extrimely fast.

The interface is done using C++ templates so it is generic and reusable on any mex-application that needs these pointers.

Interface provides two functions: mxSetClassPr and mxGetClassPr.

-mxSetClassPr transforms class pointers to mxArray* which is of type UINT64.
-mxGetPr is the inverse of mxSetClassPr and thus casts mxArray of type UINT64 to given Class type pointer.

Package contains the interface and a very simple (and nonrobust) example showing how to use the interface matlab_c_ptr.

Installation:
-Unzip the package (containing 4 files: matlab_c_ptr.h, classtest1.cpp, classtest2.cpp and dummyclas.h) to a folder.
- Set the current folder to the folder where files were extracted and use mex command to compile the files classtest1.cpp and classtest2.cpp.

Now the example files classtest1.cpp and classtest2.cpp DO NOT do any error checkings and should be used exactly as shown here:
dummyadress = classtest1(6)

dummyadress =

998008912

>> value = classtest2(dummyadress)

value =

6

Cite As

Quant Guy (2024). API to transfer references of C-classes to Matlab and back (https://www.mathworks.com/matlabcentral/fileexchange/29042-api-to-transfer-references-of-c-classes-to-matlab-and-back), MATLAB Central File Exchange. Retrieved .

MATLAB Release Compatibility
Created with R2010b
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.0.0