C++ vector to MxArray as output for a MEX function

조회 수: 7 (최근 30일)
Nicolas Jaccard
Nicolas Jaccard 2011년 11월 4일
Hi all,
I've been stuck on this for a few days; I have a function with many nested for-loops and I decided to create a MEX version in order to speed up its execution. So far so good, the compiled approach is, as expected, much faster.
I generate a non determinate number of data points that I store in an array. For now, I am creating a very large array hoping it will be big enough to store my data. But I would like it to be a tad more elegant and use dynamic arrays. Because I am adding one element at a time, I figured that re-allocating every iteration is not the most optimal approach. I then tried to use vectors (as in C++ vectors) but I ran into another issue:
I have the following:
vector<int> myVector(0);
To which I add elements using:
myVector.push_back(dataPoint);
My question is: how can I possibly copy the content of the vector at the end of my routine to an mxArray that I can thenuse as the output of my MEX function? I tried memcopy but somehow I keep getting seg faults.
Any help?
Thanks!
Nicolas
  댓글 수: 2
James Tursa
James Tursa 2011년 11월 4일
Please show the code you are using to do the copying.
Kaustubha Govind
Kaustubha Govind 2011년 11월 4일
Also, please show us how you are constructing the mxArray.

댓글을 달려면 로그인하십시오.

채택된 답변

Nicolas Jaccard
Nicolas Jaccard 2011년 11월 4일
I guess this is a typical case of actually writing the question making me realize what was wrong!
I constructed my mxArray using mxCreateNumericMatrix but I used the wrong data type, in order to be compatible with my int vector, I had to use mxUINT16_CLASS as the type flag.
Nicolas

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Write C Functions Callable from MATLAB (MEX Files)에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by