/* ========================================================================== * array_of_structs_to_struct_of_arrays.c * example for illustrating how to manipulate structure and cell array * * takes a (MxN) structure matrix and returns a new structure (1x1) * containing corresponding fields: for string input, it will be (MxN) * cell array; and for numeric (noncomplex, scalar) input, it will be (MxN) * vector of numbers with the same classID as input, such as int, double * etc.. * * This is a MEX-file for MATLAB. * Copyright 1984-2006 The MathWorks, Inc. *==========================================================================*/ /* $Revision: 1.6.6.2 $ */ #include "mex.h" #include "string.h" #define MAXCHARS 80 /* max length of string contained in each field */ /* the gateway routine. */ void mexFunction( int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[] ) { const char **fnames; /* pointers to field names */ const mwSize *dims; mxArray *tmp, *fout; char *pdata=NULL; int ifield, nfields; mxClassID *classIDflags; mwIndex jstruct; mwSize NStructElems; mwSize ndim; /* check proper input and output */ if(nrhs!=1) mexErrMsgTxt("One input required."); else if(nlhs > 1) mexErrMsgTxt("Too many output arguments."); else if(!mxIsStruct(prhs[0])) mexErrMsgTxt("Input must be a structure."); /* get input arguments */ nfields = mxGetNumberOfFields(prhs[0]); NStructElems = mxGetNumberOfElements(prhs[0]); /* allocate memory for storing classIDflags */ classIDflags = mxCalloc(nfields, sizeof(mxClassID)); /* check empty field, proper data type, and data type consistency; and get classID for each field. */ for(ifield=0; ifield