|
"Anthony Lamb" <alamb@mathworks.com> wrote in message <i5eg9o$k49$1@fred.mathworks.com>...
> Hi All,
>
> I'm preparing an interface to a C++ Voronoi library and I've gotten to the point where I have the vertices for each cell. Since I have a varying number of vertices for each Voronoi cell I've opted for the Cell array structure to store the data.
> So here is where I am...for each Voronoi cell I have the coordinates stored in Vpts which is initialized using the following lines:
>
> Vpts = new double *[nVert];
> for(i=0; i< nVert ; i++) {Vpts[i] = new double [2];}
>
> where nVert is the number of vertices for each Voronoi Cell. I fill Vpts with the coordinates of each vertex with the following lines:
>
> for (vPt=0;vPt<nVert;vPt++){
> Vpts[vPt][1]= x;
> Vpts[vPt][2]=y; }
Error. Your indexing above should be 0 and 1, not 1 and 2.
>
> My question here is how do I create and store these values into a Cell structure to send back to Matlab. The syntax I used for my Matlab function is:
>
> [vIndex nID] = nodes;
>
> I need vIndex in the form of a Matlab cell array, where vIndex{i}(n,2) represents the coordinates of the n vertices for the Voronoi cell i.
>
> Any help you can offer will be greatly appreciated.
>
> Many thanks in advance,
>
> Tony
Here is an outline code to get you started. Call it with no arguments and it will return a cell array that I think matches your requested format.
#include "mex.h"
#define NVERT 10
#define NCELL 5
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
int j = 1;
int vPt, nVert = NVERT;
double Vpts[NVERT][2];
double *pr;
mxArray *mx;
for( vPt=0; vPt<nVert; vPt++ ){ // fill some sample data
Vpts[vPt][0] = j++;
Vpts[vPt][1] = j++;
}
plhs[0] = mxCreateCellMatrix(NCELL, 1); // create return cell array
for( j=0; j<NCELL; j++ ){
mx = mxCreateDoubleMatrix(nVert, 2, mxREAL);
pr = mxGetPr(mx);
for( vPt=0; vPt<nVert; vPt++ ) { // fill in return cell
*pr++ = Vpts[vPt][0];
}
for( vPt=0; vPt<nVert; vPt++ ) { // fill in return cell
*pr++ = Vpts[vPt][1];
}
mxSetCell(plhs[0], j, mx); // attach it to cell array
}
}
James Tursa
|