Passing WORD data type from C file to mex function:

1 次查看(过去 30 天)
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

采纳的回答

James Tursa
James Tursa 2013-3-1
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 个评论
James Tursa
James Tursa 2013-3-1
编辑:James Tursa 2013-3-1
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];
}
Alexei Halpin
Alexei Halpin 2013-3-1
Thanks James, turns out we tried that prior, and the reason why it didn't work was that we neglected to initialize a hardware parameter..ha ha. sorry to have troubled you with the follow-up question, but really appreciate that you took the time!

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 MATLAB Compiler SDK 的更多信息

标签

产品

Community Treasure Hunt

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

Start Hunting!

Translated by