The Math Forum

Search All of the Math Forum:

Views expressed in these public forums are not endorsed by NCTM or The Math Forum.

Math Forum » Discussions » Software » comp.soft-sys.matlab

Notice: We are no longer accepting new posts, but the forums will continue to be readable.

Topic: Pass structure data using mex
Replies: 2   Last Post: Dec 30, 2012 4:19 PM

Advanced Search

Back to Topic List Back to Topic List Jump to Tree View Jump to Tree View   Messages: [ Previous | Next ]

Posts: 6
Registered: 12/28/12
Pass structure data using mex
Posted: Dec 28, 2012 6:27 PM
  Click to see the message monospaced in plain text Plain Text   Click to reply to this topic Reply

Hi there, I'm writing a c-mex code to output a structure data from a structure matrix. The code works ok in the sense that I can see the returning data in Matlab. But when I run clear or run the code again, it just crashes the matlab.

I used a 1x3 structure matrix as input:




and the c code I used looks like this:

#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;

/* 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);

const int namedim[2]={1,1};

tmp_f1 = mxGetFieldByNumber(prhs[0],nstruct,0);
tmp_f2 = mxGetFieldByNumber(prhs[0],nstruct,1);
tmp_f3 = 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);

After running, I can see input(1) exactlly in matlab. But I cannot run command like clear or something that might change the memory.

Is there anyone might have any idea about this problem?

Point your RSS reader here for a feed of the latest messages in this topic.

[Privacy Policy] [Terms of Use]

© The Math Forum at NCTM 1994-2018. All Rights Reserved.