Passing WORD data type from C file to mex function:
1 view (last 30 days)
Show older comments
Hello, I have a quick question regarding converting between data types in a mex file. I am interfacing to some hardware which calls a WORD pointer in its measuring function, but I'm having trouble passing this quantity back to Matlab as the output appears as a bunch of zeros. Below is a truncated version of the code to highlight the problem I'm having, I've removed the overhead because the code compiles and I'm fairly certain my problems are coming from the mexfunction part:
#include <windows.h>
#include "DCamUSB.h"
#include "DCamStatusCode.h"
#include "mex.h"
extern void _main();
int mainsubr(long *RetVal, double *data)
{
WORD* pDataBuff = NULL;
pDataBuff = new WORD[ nImageSize * nFrameCount];
// Acquisition execute, camera acquires data
*data = pDataBuff;
for( nCountH = 0; nCountH < nHeight; nCountH++ )
{
for( nCountW = 0; nCountW < nWidth; nCountW++ )
{
printf("%d\n", *data);
data ++;*
}
}
break;
}
// Process for exit.
DcamStop(); // Stop acquisition
delete [] pDataBuff; // Release acquisition buffer
// status
return 0;
}
// ---------------------------- MEX PART --------------------------------
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
WORD *Buffer; // measured spectra
long mrows2 = 2068, mcols2 = 1;
plhs[0] = mxCreateDoubleMatrix(mrows2, mcols2, mxREAL);
Buffer = (WORD *)mxGetData(plhs[1]);
// call main subroutine
mainsubr(Status, Buffer);
return;
}
end
The issue I suppose is the way I am calling mxGetData, but I'm not certain why it is only returning zeros. The little printf command I have in the for loop in the main subroutine is displaying the values I want in the Matlab shell, but someone I am not able to then pass these values to the array in Matlab. Any tips? I'm assuming this has something to do with recasting the WORD * into a double * somehow but it seems (according to my compiler) that this is not possible, and therefore the conversion has to take place in the mex function I suppose. Thanks for any tips you can provide.
Alexei
0 Comments
Accepted Answer
James Tursa
on 1 Mar 2013
I don't see any code where you put the results of your acquisition into data. All I see is you stuffing the pointer into the first location, which btw seems vary suspect since you are converting a pointer to a double. Also, simply putting a (WORD *) in front of the mxGetData call does not change the fact that the underlying storage is doubles. Your mainsubr has the 2nd argument as a (double *) anyway, so what is the point of this? I suspect you want something like this instead (assumes WORD is a 2-byte unsigned data type):
plhs[0] = mxCreateNumericMatrix(mrows2, mcols2, mxUINT16_CLASS, mxREAL);
:
int mainsubr(long *RetVal, WORD *data)
:
*data = pDataBuff; // delete this line
:
*data = pDataBuff[appropriate index]; // add this inside inner loop
But why are you allocating pDataBuff in the first place? Why can't you just have your data acquision put the results into data directly instead of doing a copy?
3 Comments
James Tursa
on 1 Mar 2013
Edited: James Tursa
on 1 Mar 2013
You need to run pDataBuff through all the values. E.g., instead of your double loop you could just use a single loop like this:
for( i=0; i<nFrameCount*nWidth; i++ ) {
data[i] = pDataBuff[i];
}
More Answers (0)
See Also
Categories
Find more on MATLAB Compiler SDK in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!