C/C++ and Matlab types convertor

Version 1.9.0.0 (65.3 KB) by sun peng
Bidirectional conversion between C/C++ type (STL, openCV) and Matlab matrix (compile or run time)
6.9K Downloads
Updated 29 Oct 2013

View License

Title: C/C++ <-> Matlab types convertor
Brief: Bidirectional conversion between C/C++ types (native, STL, openCV...) and Matlab matrix (compile or run time).
Key words: C, C++, mxArray, OpenCV, IplImage, iterator, mex, engine

Description:

Consider below scenarios (especially when programming with signal/speech/image processing and scientific computation):
1 Dumping C/C++ data into Matlab workspace in run-time to visualize data and facilitate debugging. But calling engine APIs directly seems not to be that convenient.
2 Implementing underlying algorithm as mex file to accelarate m file. Each time you must convert mxArray to C/C++ types, do some job, and finally convert C/C++ types back to returned mxArray.

This project provides easy access to above tasks given appropriate iterators (refer to any C++ STL textbook if iterator seems nothing to you).

As to scenario 1 see below examples:

/**************************
* EXAMPLE A for scenario 1
**************************/
unsigned char rgb_img[3*256*126]; // 3D signal, i.e. color image
// initialize rgb_img and do something to it...
const char* command = "figure; imshow(I)";

// Dump it as 3D matrix named I. column(width):256?row(height):126 and page(channel):3,
// then view it using matlab function "imshow".
matlab << name ("I")
<< width (256) << height (126) << channel (3)
<< start (rgb_img)
<< cmd (command);

Dump 1D and 2D signal goes similarly. Currently maximum 3D is supported.
Also openCV is supported. For example:

/**************************
* EXAMPLE B for scenario 1
**************************/
IplImage* pimg; // openCV image types
// initialize p and do something to it...

// Dump pimg as matrix I. The size and types are made the same as pimg automatically.
// Then view it.
matlab << name("I") << var(pimg) << cmd("figure;imshow(I)");

As to scenario 2 see below example:

/**************************
* EXAMPLE A for scenario 2
**************************/
void mexFunction(int nlhs, mxArray *plhs[],
int nrhs, const mxArray *prhs[])
{
vector<double> vt;
double val;
int n = (int) mxGetNumberOfElements(prhs[0]);
vt.resize(n);
// 1. convert mxArray to C/C++ types
mat_to_values (prhs[0], vt.begin());
mat_to_scalar (prhs[1], &val);

// 2. do some job to vt and val

// 3. finally convert C/C++ types back to mxArray
plhs[0] = values_to_new_mat (vt.begin(),vt.end());
}

mat_to_values and values_to_new_mat take pointer/iterator as input arguments, with appropriate iterator in hand you can also convert C native array, std::list or any other user data types (e.g. OpenCV IplImage).

For details, see more examples and the document in the zip file.
Have fun:)

Note:
To compile the codes with VC10, you need add _SECURE_SCL=1 to COMPFLAGS into mexopts.bat file, or you will get the errors:

c:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\INCLUDE\xutility(287) : error C2582: 'operator '='

References
1. http://stackoverflow.com/questions/9357699/error-c2582-operator-function-is-unavailable-in-bitstreambitset-extracto/16558308#16558308

2.
http://preshing.com/20110807/the-cost-of-_secure_scl

Courtesy to SenyaLab (http://www.mathworks.cn/matlabcentral/fileexchange/authors/195611) on this issue!

Cite As

sun peng (2024). C/C++ and Matlab types convertor (https://www.mathworks.com/matlabcentral/fileexchange/20927-c-c-and-matlab-types-convertor), MATLAB Central File Exchange. Retrieved .

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

Inspired by: C++ raw data dumper

Inspired: Search( filetype,folderpath)

Community Treasure Hunt

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

Start Hunting!

mc_ext_/example/dump2/

mc_ext_/example/opencv_CvMat_mex/

mc_ext_/example/opencv_mex/

mc_ext_/example/simple_mex/

Version Published Release Notes
1.9.0.0

Adapt to OpenCV 2.x ("cxtypes.h" no longer exists)

1.8.0.0

Guidance to compiling with VC10

1.7.0.0

* support for mxLOGICAL_CLASS conversion is added

1.6.0.0

* Codes involving typename are modified to make it work with GCC
(Thank Jack. Z.G.Tan (zgtan(at)eee(dot)hku(dot)hk) for pointing it out:) )

1.4.0.0

Modification on typename/typedef syntex to make it work with GCC
This job attributes to Martin Dale(http://www.mathworks.com/matlabcentral/fileexchange/authors/36049)
Thanks for his constant focus and constructive suggestions on this project:)

1.3.0.0

* A bug in pix_iter_rgb::end() is fixed
(Thank Aviv Hurvitz(http://www.mathworks.com/matlabcentral/fileexchange/authors/40629) for pointing it out:) )

1.2.0.0

minor modification on document

1.1.0.0

* More types for conversion
* conversion between CvMat and mxArray (2D currently)
(All above are due to Dale Martin)
* Explicit function name. The old names are still valid but deprecated

1.0.0.0

Minor modification on READEME