Hi everyone,
I have a mex function in C that internally generates an unpredictably large volume of data to be output back to Matlab. I would like the return to be a Struct Matrix of the 1 by N, where N is the unknown large number. The memory should be allocated by chunks, so that in case of insufficient memory the calculation can be stopped and the return that does fit into memory handled safely back to Matlab.
It seems that the combination of mxSetN() and mxRealloc() is a proper combination, but I can't find sufficient info on how to use mxRealloc to allocate space for additional elements of structural matrix.
Here is a minimal code example of what I'm trying to do
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) { mwSize i; mxArray *mxp; nlhs[0]=mxCreateStructMatrix(1,1,0,NULL); //structure with one element and no fields for now while (continue calculation) { //one more round of calculations that creates a lot of output //stored in structure g with double* field1,..,K mxAddField(nlhs[0],"field1"); mxp=mxCreateDoubleMatrix(appropriate,size,mxREAL); if (mxp==NULL) {mexErrMsgTxt("Failed to allocate memory!")} mxSetField(nlhs[0],i,"field1",mxp); ... mxAddField(nlhs[0],"fieldK"); mxp=mxCreateDoubleMatrix(appropriate,size,mxREAL); if (mxp==NULL) {mexErrMsgTxt("Failed to allocate memory!")} mxSetField(nlhs[0],i,"fieldK",mxp); i++; mxSetM(nlhs[0],i) //What then? } }
Best Answer