如何在MEX函数中读取整数数组 [英] how to read an integer array in MEX-function
问题描述
我正在将uint8类型的整数数组从MATLAB传递给MEX函数.如何读取这些值?我尝试使用:
I am passing an integer array of uint8 type from MATLAB to a MEX-function. How do I read these values? I tried using:
int *n;
n = (int * ) mxGetData(prhs[0]);
但这些值显示为垃圾.我也尝试过
but the values come out as junk. I also tried
double *n;
n= mxGetPr(prhs[0]);
,在这种情况下,也会出现垃圾值.
and in this case junk values also appear.
对此有什么解决方案?
基本上我想在MEX函数中读取整数值,但mxGetPr
返回双精度类型.
Basically I want to read the integer value in the MEX-function but mxGetPr
returns double type.
推荐答案
看一下演示MEX函数explorer.c,您可以使用MATLAB在pEX中打开它
Take a look at the demo MEX-function explore.c, which you can open in MATLAB using
edit([matlabroot '/extern/examples/mex/explore.c']);
在这里您会发现一堆函数,它们的名称都以analyze_
开头,然后是类型(例如,analyze_uint8
).在这些函数中,您将看到对mxGetData
的调用的输出被强制转换为特定的C类型,如下所示:
In there you'll find a bunch of functions whose names all start with analyze_
and then a type (for example, analyze_uint8
). In those functions you'll see the output of calls to mxGetData
being cast to a particular C type, like this:
pr = (unsigned char *)mxGetData(array_ptr);
pr
现在指向array_ptr
的实部,即unsigned char
的数组.
pr
now points to the real part of array_ptr
, an array of unsigned char
.
这篇关于如何在MEX函数中读取整数数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!