我有以下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 array (inNoise),但对于inGlucose,它会返回一个0数组。
在Matlab 2018A中似乎有解决这个问题的方法,但由于不可避免的原因,我只能找到2016B。
有人能帮我吗?
发布于 2018-08-18 06:56:15
float (*inGlucose)[12]是一个指向32个大浮点数的指针数组。您将获取一个双精度浮点值的数组,并将其解释为指针数组。奇怪的是,程序没有严重崩溃。
取而代之的是,获取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简单,因为您已经建立了它是一个双精度数组。
发布于 2018-08-18 07:06:37
为什么不干脆
float * inGlucose= (float *)(mxGetData(prhs[1]));只有当您的第二个输入是float (或MATLAB中的single )时,这才有效,因此您还需要更改输入错误检查代码。但是,您在注释中坚持认为第二个输入是一个浮点数。
当你调用这段代码时,只需确保你这样做
mymexfile(... , single(glucose), ..., ...)
https://stackoverflow.com/questions/51903503
复制相似问题