Passing WORD data type from C file to mex function:

1 Ansicht (letzte 30 Tage)
Alexei Halpin
Alexei Halpin am 28 Feb. 2013
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

Akzeptierte Antwort

James Tursa
James Tursa am 1 Mär. 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 Kommentare
James Tursa
James Tursa am 1 Mär. 2013
Bearbeitet: James Tursa am 1 Mär. 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];
}
Alexei Halpin
Alexei Halpin am 1 Mär. 2013
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!

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu MATLAB Compiler SDK finden Sie in Help Center und File Exchange

Tags

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by