Drexel dragonThe Math ForumDonate to the Math Forum



Search All of the Math Forum:

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


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

Topic: mexCallMATLAB crashes in loops?
Replies: 12   Last Post: Jan 3, 2013 1:49 PM

Advanced Search

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

Posts: 6
Registered: 12/28/12
mexCallMATLAB crashes in loops?
Posted: Dec 31, 2012 5:49 PM
  Click to see the message monospaced in plain text Plain Text   Click to reply to this topic Reply

Hello everyone, I'm working on a mex code that using mexCallMATLAB several times in a loop. The input is supposed to be a structure matrix and the matlab function called in the mex code dealing with a single structure data.

The matlab fuction called in mex is like this:
<simple.m>
function y=simple(x)
y=zeros(3,1);
y(1)=x.field_2+1;
y(2)=2;
y(3)=3;
</simple.m>

The mex code I wrote:

#include "mex.h"
#include "math.h"
#include <stdlib.h>

void mexFunction(int nlhs, mxArray *plhs[],int nrhs, mxArray *prhs[])
{
const char **fnames;
const int *coutsize;
int ifield, nfields, ninputs;
double *loopout;
double *cout;
double n1,n2;

mwSize structdims[2]={1,1};
mwSize outputdims[2];
mwIndex istruct;
mxArray *incall[1];
mxArray *tmp;
mxArray *mout[1];


/* Check for proper number of arguments */
if (nrhs != 1) {
mexErrMsgTxt("One input argument required.");
} else if (nlhs > 1) {
mexErrMsgTxt("Too many output arguments.");
} else if ((mxIsStruct(prhs[0]) != 1)){
mexErrMsgTxt("Input must be structure");
}

/* Create single struct array */
nfields = mxGetNumberOfFields(prhs[0]);
ninputs = mxGetN(prhs[0]);
fnames = (const char **)mxCalloc(nfields, sizeof(fnames));
for(ifield=0; ifield<nfields; ifield++){
fnames[ifield] = mxGetFieldNameByNumber(prhs[0],ifield);}
incall[0] = mxCreateStructArray(2,structdims , nfields, fnames);
mxFree((void *)fnames);

for(istruct=0;istruct<ninputs;istruct++){

for(ifield=0; ifield<nfields; ifield++){
tmp=mxDuplicateArray(mxGetFieldByNumber(prhs[0],istruct,ifield));
mxSetFieldByNumber(incall[0], istruct, ifield, tmp);
}

mexCallMATLAB(1, mout, 1, incall, "simple");

cout = mxGetPr(mout[0]);
coutsize=mxGetDimensions(mout[0]);

if(istruct==0){
mexPrintf("Initializing...\n");
n1 = coutsize[0];
n2 = 2 * n1;

/* Create Output Value */
outputdims[0] = n2;
outputdims[1] = ninputs;
plhs[0]=mxCreateNumericArray(2, outputdims, mxDOUBLE_CLASS, mxREAL);
loopout = mxGetPr(plhs[0]);
}

for(int it=0;it<coutsize[0];it++){
loopout[(int)istruct*(int)n2+it] = 2*cout[it];
}

mexPrintf("now:%d\n",istruct);
}

//Clean up
mxDestroyArray(incall[0]);
mxDestroyArray(mout[0]);
mxDestroyArray(tmp);
}

The input structure matrix I used to test the code was:
input(1).field_1='a1';
input(1).field_2=1.11;
input(1).field_3=[1,1,1];

input(2).field_1='a2';
input(2).field_2=2.22;
input(2).field_3=[2,2,2];

input(3).field_1='a3';
input(3).field_2=3.33;
input(3).field_3=[3,3,3];

When there is no loop, everything worked well. As long as I add the loop in, matlab crashes. There might be errors around dimensions or output settings that I'm not able to figure out. Could you give me any suggestions?

Best regards



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

[Privacy Policy] [Terms of Use]

© Drexel University 1994-2014. All Rights Reserved.
The Math Forum is a research and educational enterprise of the Drexel University School of Education.