I would like to implement STL containers with my C++ shared library project, for example the STL vector, with the mwArray API. I want to do this for performance reasons.
MATLAB: How to use STL containers in the C++ shared library
clibraryMATLAB Compilersharedstlvector
Related Solutions
It is possible to create dynamic arrays with the mwArray class in MATLAB Compiler 4.1 (R14SP1). Below is an example (testme.cpp) that will let the user define the size of 2 arrays:
#include "mclcppclass.h"int main(){ if (!mclInitializeApplication(NULL,0)) { std::cerr << "could not initialize the library properly"<<std::endl; return -1; } try { int x,y; int* a = NULL; int* b = NULL; std::cout << "Enter length of array 1:" <<std::endl; std::cin >> x; // Create input data std::cout << "Enter length of array 2:" <<std::endl; std::cin >> y; a = new int[x]; // Allocate x ints and save ptr in a. b = new int[y]; // Allocate y ints and save ptr in b. for (int i=0; i<x; i++) { a[i] = i; // Initialize all elements in increments of 1. } for (int j=0; j<y; j++) { b[j] = 1; // Initialize all elements to one. } mwArray in1(1, x, mxDOUBLE_CLASS, mxREAL); mwArray in2(1, y, mxDOUBLE_CLASS, mxREAL); in1.SetData(a, x); in2.SetData(b, y); std::cout << "The 1st array is:" << std::endl; std::cout << in1 << std::endl; std::cout << "The 2nd array is:" << std::endl; std::cout << in2 << std::endl; delete [] a; //When done free memory pointed to by a. a = NULL; delete [] b; b = NULL; //When done, free memory pointed to by b. } catch (const mwException& e) { std::cerr << e.what() << std::endl; return -1; } catch (...) { std::cerr << "Unexpected error thrown" << std::endl; return -1; } mclTerminateApplication(); return 0; }
You can compile this example using the following command:
mbuild -v testme.cpp
Generally with any segmentation violation, you're supposed to contact the Mathworks technical support directly.
Best Answer