我有以下 matlab mex
函数:
void mexFunction( int nlhs, mxArray *plhs[],int nrhs, const mxArray *prhs[] ){
//Declare variables for the input arguments.
size_t lengthh; /* input scalar */
double *inNoise; /* 1xN input matrix */
float (*inGlucose)[12]; // 1xN input matrix float (*PatchSet)[64];
double *inDates; /* 1xN input matrix */
if(nrhs != 4) {
mexErrMsgIdAndTxt("MyToolbox:fullLoop:nrhs", "4 inputs required.");
}
/*if(nlhs != 1) {
mexErrMsgIdAndTxt("MyToolbox:arrayProduct:nlhs",
"One output required.");
}*/
(void) nlhs; (void) plhs;
// make sure the first input argument is an array
if( mxIsComplex(prhs[0]) || !mxIsDouble(prhs[0]) ) {
mexErrMsgIdAndTxt("MyToolbox:fullLoop:notDouble","1:Input matrix must be type double.");
}
if( !mxIsDouble(prhs[1]) || mxIsComplex(prhs[1]) ) {
mexErrMsgIdAndTxt("MyToolbox:fullLoop:notDouble","2:Input matrix must be type double.");
}
if( !mxIsDouble(prhs[2]) || mxIsComplex(prhs[2])|| mxGetNumberOfElements(prhs[2]) < 1 ) {
mexErrMsgIdAndTxt("MyToolbox:arrayProduct:notDouble","3:Input matrix must be type long int.");
}
if( mxIsComplex(prhs[3]) || mxGetNumberOfElements(prhs[3])!=1 ) {
mexErrMsgIdAndTxt("MyToolbox:arrayProduct:notDouble","4:Input scalar must be type double.");
}
// check that number of rows in second input argument is 1
if(mxGetM(prhs[0])!=1) {
mexErrMsgIdAndTxt("MyToolbox:arrayProduct:notRowVector","5:1st argument must be a row vector.");
}
if(mxGetM(prhs[1])!=1) {
mexErrMsgIdAndTxt("MyToolbox:arrayProduct:notRowVector","6:2nd argument must be a row vector.");
}
if(mxGetM(prhs[2])!=1) {
mexErrMsgIdAndTxt("MyToolbox:arrayProduct:notRowVector","7:3rd argument must be a row vector.");
}
printf("Sizes: %d %d %d \n", mxGetNumberOfElements(prhs[0]), mxGetNumberOfElements(prhs[1]), mxGetNumberOfElements(prhs[2]));
/******************************************PROBLEM*****************************************************/
inNoise = (double *)mxGetData(prhs[0]);//mxGetData
inGlucose = (float (*)[12]) mxGetData(prhs[1]);//PatchSet = (float (*)[64]) mxGetData(prhs[0]);
lengthh = mxGetScalar(prhs[3]);
/******************************************************************************************************/
printf("length: %d\n",lengthh);
int i;
for (i=0;i<lengthh;i++){
printf("%f -- %f\n",inGlucose[i],inNoise[i]);
}
//free(gStat);
return;
}
问题出在由星号线分隔的部分中。
我关注了这个tutorial来转换数组。 尽管程序可以正确读取 double 组 (inNoise),但它会为 inGlucose 返回一个由 0 组成的数组。
Matlab 2018A 中似乎对此有解决方案,但由于不可避免的原因我只能找到 2016B。
有人可以帮我吗?
最佳答案
float (*inGlucose)[12]
是指向 32 位 float 的指针数组。您正在获取一个 double 浮点值数组并将其解释为一个指针数组。奇怪的是程序没有严重崩溃。
相反,如果您需要这种格式的值,请采用 double*
并将值复制到 float
数组。
double *inGlucoseD;
float inGlucose[12];
//...
inGlucoseD = mxGetPr(prhs[1]);
for (int ii=0; ii<12; ++ii) {
inGlucose[ii] = inGlucoseD[ii];
}
还要进行测试以确保您有 12 个要复制的输入值...
请注意,这里的 mxGetPr
比 mxGetData
更简单,因为您已经确定它是一个 double 组。
关于c - mexGetData() 输出零,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51903503/