|
|
Re: Pass structure data using mex
Posted:
Dec 29, 2012 3:55 AM
|
|
You have to make deep copy of the field because it belongs to another variable. Also you do not need to create the fields.
Bruno
#include "mex.h" #include "matrix.h"
void mexFunction(int nlhs, mxArray *plhs[],int nrhs, const mxArray *prhs[]) { const char **fnames; int ifield, nfields; mwSize dims[2]={1,1}; mwIndex nstruct=0; mxArray *tmp_f1, *tmp_f2, *tmp_f3; const int namedim[2]={1,1}; /* Check for proper number of arguments */ if (nrhs != 1) { mexErrMsgTxt("One input arguments required."); } else if (nlhs > 1) { mexErrMsgTxt("Too many output arguments."); } else if ((mxIsStruct(prhs[0]) != 1)){ mexErrMsgTxt("Input must be a structure matrix."); } nfields = mxGetNumberOfFields(prhs[0]); fnames = (const char **)mxCalloc(nfields, sizeof(fnames)); for(ifield=0; ifield<nfields; ifield++) { fnames[ifield] = mxGetFieldNameByNumber(prhs[0],ifield); } plhs[0] = mxCreateStructArray(2, dims, nfields, fnames); mxFree((void *)fnames); tmp_f1 = mxDuplicateArray(mxGetFieldByNumber(prhs[0],nstruct,0)); tmp_f2 = mxDuplicateArray(mxGetFieldByNumber(prhs[0],nstruct,1)); tmp_f3 = mxDuplicateArray(mxGetFieldByNumber(prhs[0],nstruct,2)); mxSetFieldByNumber(plhs[0], nstruct, 0, tmp_f1); mxSetFieldByNumber(plhs[0], nstruct, 1, tmp_f2); mxSetFieldByNumber(plhs[0], nstruct, 2, tmp_f3); }
|
|