Issue while passing array data other than double data type from C to Matlab using MexFunction
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
I am using Malab Mexfunction to pass two kinds of variables (DOUBLE and INTEGER) from C to Matlab. I dont face any issue while compiling. However, the returned values are not as per expectation. It seems Mexfunction only transfer DOUBLE type arrays properly in Matlab from C. For arrays other than DOUBLE type the recieved data in Matlab is incorrect. I have tried the appended code on Matlab 2011a(32-bit) , 2012a(64-bit) and 2014a(64-bit). I use Microsoft Windows SDK 7.1 compiler for C. My PC is 64-bit Windows 7 professional.
The code is as follows:
% File name : test
#include "mex.h"
void mexFunction(int nlhs,mxArray *plhs[],int nrhs,const mxArray *prhs[]){
mwSize dims[2];
unsigned int len=2, width=4, i, j;
unsigned int *out1;
double *out2;
if(nrhs!=0) mexErrMsgIdAndTxt("MATLAB:Test:invalidNumInputs",
"Zero input required.");
if(nlhs!=2) mexErrMsgIdAndTxt("MATLAB:Test:invalidNumOutputs",
"Two output required.");
dims[0] = len; dims[1] = width;
plhs[0] = mxCreateNumericArray(2,dims,mxUINT16_CLASS,mxREAL);
out1 = (unsigned int *)mxGetData(plhs[0]);
plhs[1] = mxCreateDoubleMatrix(len,width,mxREAL);
out2 = mxGetPr(plhs[1]);
for(i=0;i<len;i++)
for(j=0;j<width;j++){
*(out1 + i*width + j) = 2;
*(out2 + i*width + j) = 2.0;
}
mexPrintf("\nInteger Array\n");
for(i=0;i<len;i++){
for(j=0;j<width;j++)
mexPrintf("[%d][%d]=%d ",i,j,*(out1 + i*width + j));
mexPrintf("\n");
}
mexPrintf("\nDouble Array\n");
for(i=0;i<len;i++){
for(j=0;j<width;j++)
mexPrintf("[%d][%d]=%f ",i,j,*(out2 + i*width + j));
mexPrintf("\n");
}
}
I get the following output
[a,b]=test
Integer
[0][0]=2 [0][1]=2 [0][2]=2 [0][3]=2
[1][0]=2 [1][1]=2 [1][2]=2 [1][3]=2
Double
[0][0]=2.000000 [0][1]=2.000000 [0][2]=2.000000 [0][3]=2.000000
[1][0]=2.000000 [1][1]=2.000000 [1][2]=2.000000 [1][3]=2.000000
a =
2 2 2 2
0 0 0 0
b =
2 2 2 2
2 2 2 2
whos
Name Size Bytes Class Attributes
a 2x4 16 uint16
b 2x4 64 double
0 Kommentare
Antworten (1)
Philip Borghesani
am 22 Jan. 2015
Bearbeitet: Philip Borghesani
am 22 Jan. 2015
Problem here:
plhs[0] = mxCreateNumericArray(2,dims,mxUINT16_CLASS,mxREAL);
out1 = (unsigned int *)mxGetData(plhs[0]);
mxUINT16_CLASS is not an unsigned int in any 32 or 64 bit compiler. The most portable code is to use:
plhs[0] = mxCreateNumericArray(2,dims,mxUINT16_CLASS,mxREAL);
out1 = (uint16_t)mxGetData(plhs[0]); // or use unsigned short if your compiler can't find uint16_t
Or
plhs[0] = mxCreateNumericArray(2,dims,mxUINT32_CLASS,mxREAL);
out1 = (uint32_t)mxGetData(plhs[0]); // or use unsigned int if your compiler can't find uint32_t
Fix the type of out1 as needed.
For mex files the types uint32_T, uint16_T and similar are also defined to help with portability.
0 Kommentare
Siehe auch
Kategorien
Mehr zu Write C Functions Callable from MATLAB (MEX Files) finden Sie in Help Center und File Exchange
Produkte
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!