matlab internal problem and needs to close - reading from dll in MEX

2 views (last 30 days)
I am working a a project where I have to make a Matlab interface for IR tracking software. (www.free-track.net). FreeTrack provides a SDK in their download with Matlab code which makes a MEX file from C code. The C code reads from a dll called "freetrackClient.dll", which holds the tracking data. When I run FreeTrack and track my head movement, the data of my movement can be seen on the screen.
Now I want to read this data and save it into Matlab. However, when i run the funciton getData, Matlab crashes and says: "MATLAB has encountered a problem and needs to close"
?Why can't i read from the DLL?
  2 Comments
Ken Atwell
Ken Atwell on 15 Feb 2012
Are you using loadlibrary? Can you add a code fragment (i.e., the MATLAB code you are running that causes the crash)?
Ezra
Ezra on 15 Feb 2012
I have the MEX file Freetrack and in Matlab I do
>> Freetrack("getData")
which runs the function "doGetData" in my generated from C, MEX file.
The function "doGetData" checks if there is data the following way:
if (getData(pData)) //call dll function to fill 'data'
{
mexPrintf("Getting data from dll\n");
//copy the returned data into the output variable
mxSetField(plhs[0],0,fieldnames[0], mxCreateDoubleScalar(data.yaw));
mxSetField(plhs[0],0,fieldnames[1], mxCreateDoubleScalar(data.pitch));
mxSetField(plhs[0],0,fieldnames[2], mxCreateDoubleScalar(data.roll));
mxSetField(plhs[0],0,fieldnames[3], mxCreateDoubleScalar(data.x));
mxSetField(plhs[0],0,fieldnames[4], mxCreateDoubleScalar(data.y));
mxSetField(plhs[0],0,fieldnames[5], mxCreateDoubleScalar(data.z));
mxSetField(plhs[0],0,fieldnames[6], mxCreateDoubleScalar(data.dataID));
}
else
{
//display a warning that there was no data
mexPrintf("Nothing returned from FTGetData\n");
}

Sign in to comment.

Answers (1)

Ken Atwell
Ken Atwell on 15 Feb 2012
Double-check that you've initialized plhs[0] with mxCreateStructArray with at least the seven fields that you reference later one.
When in doubt, start with a working example and modify incrementally from there:
edit([matlabroot '/extern/examples/mx/mxcreatestructarray.c']);
  1 Comment
Ezra
Ezra on 16 Feb 2012
Thanks for your comment.
I initialize plhs[0] the following:
const char *fieldnames[] ={"yaw", "pitch", "roll", "x","y","z","dataID"};
plhs[0] = mxCreateStructMatrix(1,1,sizeof(fieldnames)/sizeof(char*), fieldnames);

Sign in to comment.

Categories

Find more on Write C Functions Callable from MATLAB (MEX Files) in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!