API to transfer references of C-classes to Matlab and back
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
Platform Compatibility
Windows macOS LinuxCategories
- MATLAB > External Language Interfaces > C++ with MATLAB > Call C/C++ MEX Functions from MATLAB > Write C++ Functions Callable from MATLAB (MEX Files) >
Tags
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Discover Live Editor
Create scripts with code, output, and formatted text in a single executable document.
Version | Published | Release Notes | |
---|---|---|---|
1.0.0.0 |