使用mxGetPr和mxGetData [英] Using mxGetPr vs mxGetData
问题描述
我正在尝试编写一个简单的mex函数.我有一个整数输入,它是我的对象数.
当我编译myMEX_1.cpp
并由MATLAB用任何输入值调用它时,我总是得到:
I am trying to write a simple mex function. I have an integer input which is the number of my objects.
When I compile myMEX_1.cpp
and call it by MATLAB with any input value, I always get:
请求的对象数:= 0
Number of Requested Objects := 0
但是myMEX_2.cpp
可以正常工作,并显示从MATLAB命令窗口输入的数字.
我在myMEX_1.cpp
中的错误在哪里?
But the myMEX_2.cpp
works fine and show the number inputted from MATLAB command window.
Where is the my mistake in myMEX_1.cpp
?
我的环境:MATLAB R2013a和Microsoft SDK 7.1编译器.
// myMEX_1.cpp
#include "mex.h"
void mexFunction(int nlhs, mxArray *plhs[],
int nrhs, const mxArray *prhs[])
{
char str11[100];
unsigned short frameCount;
//unsigned short *frameCountPtr;
frameCount = (*((unsigned short*)mxGetData(prhs[0])));
sprintf(str11, "Number of Requested Objects := %d:\n", frameCount);
mexPrintf(str11);
}
// myMEX_2.cpp
#include "mex.h"
void mexFunction(int nlhs, mxArray *plhs[],
int nrhs, const mxArray *prhs[])
{
char str11[100];
unsigned short frameCount;
double* dblPointer;
dblPointer = mxGetPr(prhs[0]);
frameCount = (unsigned short)(*dblPointer);
sprintf(str11, "Number of Requested Objects := %d:\n", frameCount);
mexPrintf(str11);
}
推荐答案
mxGetData
返回一个void
指针,该指针必须强制转换为 correct 数据类型的指针.
mxGetData
returns a void
pointer which must be cast to a pointer of the correct datatype.
在C中,
mxGetData
返回void
指针(void *
).由于void
指针指向没有类型的值,因此将返回值转换为与pm
In C,
mxGetData
returns avoid
pointer (void *
). Sincevoid
pointers point to a value that has no type, cast the return value to the pointer type that matches the type specified bypm
在您的情况下,我假设虽然看起来像您传入了整数,但实际上它是double
,因为这是MATLAB的默认数据类型,因此您遇到的问题是由于您尝试将其转换指向unsigned short
指针.
In your case, I'm assuming that although it looks like you've passed in an integer, it's actually a double
since that is MATLAB's default datatype so your issue is due to the fact that you try to convert it to an unsigned short
pointer.
myMEX_1(1) % Passes a double
myMEX_1(uint16(1)) % Passes an integer
要解决此问题,我们需要将mxGetData
的输出强制转换为double
指针,然后取消引用,强制转换并分配
To fix this, we would need to cast the output of mxGetData
as a double
pointer instead, and then dereference it, cast it and assign it
frameCount = (unsigned short)*(double*)mxGetData(prhs[0]);
mxGetPr
与mxGetData
除外,它会自动将mxGetData
的输出强制转换为double
指针.因此,它可以节省您一步,但仅适用于double
输入(您拥有).
mxGetPr
is the same as mxGetData
except that it automatically casts the output of mxGetData
as a double
pointer. Thus, it saves you a step but is only appropriate for double
inputs (which you have).
如果要适当地处理多种类型的输入,则需要使用 mxIsClass
.
If you want to handle inputs of multiple types appropriately, you'll need to check the type of the input using either mxIsDouble
or mxIsClass
.
if ( mxIsDouble(prhs[0]) ) {
frameCount = (unsigned short)*mxGetPr(prhs[0]);
} else if ( mxIsClass(prhs[0], "uint16") {
frameCount = *(unsigned short*)mxGetData(prhs[0]);
} else {
mexPrintf("Unknown datatype provided!");
return;
}
这篇关于使用mxGetPr和mxGetData的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!