Passing WORD data type from C file to mex function:

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

 Respuesta aceptada

James Tursa
James Tursa el 1 de Mzo. de 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 comentarios

Hi James,
Thanks a lot for your suggestion. It immediately allowed us to recover the values of the data into a Matlab array. Regarding the allocation of the pDataBuffer, it was something that was present in the manufacturer's sample code that we have been working from, I guess to account for the fact that the nFrameCount quantity can vary given input parameters from the user. But I'm unsure. One puzzling aspect now is how to read out the pDataBuffer pointer correctly, although this is more of a C++ application specific problem, read my problem :) , but maybe you have some insight on what's immediately going wrong. I don't believe this is on the mex side but perhaps I'm mistaken.
if true
// Data process
for(nCountF = 0; nCountF < nFrameCount; nCountF++)
{
for( nCountW = 0; nCountW < nWidth; nCountW++ )
{
(*data) = pDataBuff[nCountW];
data++;
}
}
end
Unfortunately for each increment of nCountF returns the same array as data as the first one. The pDataBuffer array is initialized to [nWidth*nFramecount], so I figure that within the loop the data pointer should keep reading out the pDataBuffer until the last element.
Thanks!
James Tursa
James Tursa el 1 de Mzo. de 2013
Editada: James Tursa el 1 de Mzo. de 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];
}
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!

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre MATLAB Compiler SDK en Centro de ayuda y File Exchange.

Productos

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by