我想寫一個簡單的mex函數。我有一個整數輸入,這是我的對象的數量。 當我編譯myMEX_1.cpp
並通過MATLAB與任何輸入值調用它,我總是得到:使用mxGetPr vs mxGetData
Number請求的對象:= 0
但myMEX_2.cpp
工作正常,表明從MATLAB輸入的數命令窗口。 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);
}
'mxGetPr'總是返回與輸入參數類型無關的'* double'。 – GntS
@GmtK我認爲,但只是檢查出來。雖然你的代碼可以工作,如果你真的*傳遞一個整數:'myMEX_1(uint16(1))' – Suever
函數原型在MATLAB幫助中作爲'double * mxGetPr(const mxArray * pm);' – GntS