MATLAB Mex:从 MATLAB 中的结构检索逻辑
MATLAB Mex: retrieving a logical from a struct in MATLAB
我有一个以这种方式创建的结构:
testStruct = struct; testStruct.tf = true
.
我想通过 mex 将这个结构传递到我的 c++ 代码中,这是我所做的快照:
mxArray *mxValue;
mxValue = mxGetField(prhs[0], 0, "tf");
mxLogical tf = mxGetLogicals(mxValue);
mexPrintf("tf: %i \n", tf);
无论我将 testStruct.tf
设置为 true
还是 false
,它都会打印 tf: 1
。我还使用 if 条件对其进行了测试,无论我输入什么逻辑,if 条件都会执行。
我试过 bool tf = mxGetLogicals(mxValue)
,但没用。
我能得到这方面的指导吗?
Can I get a pointer on this?
... 这就是问题所在... mxGetLogical
returns 指向 mxArray 中第一个逻辑元素的指针。 see documentation.
所以试试这个(编译为 mexTest):
#include "mex.h"
void mexFunction( int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[] )
{
mxArray *mxValue;
mxLogical *tf;
mxValue = mxGetField(prhs[0], 0, "tf");
tf = mxGetLogicals(mxValue);
mexPrintf("tf: %i \n", *tf);
}
运行 它给了我这些结果:
>> testStruct.tf = true;
>> mexTest(testStruct)
tf: 1
>> testStruct.tf = false;
>> mexTest(testStruct)
tf: 0
我有一个以这种方式创建的结构:
testStruct = struct; testStruct.tf = true
.
我想通过 mex 将这个结构传递到我的 c++ 代码中,这是我所做的快照:
mxArray *mxValue;
mxValue = mxGetField(prhs[0], 0, "tf");
mxLogical tf = mxGetLogicals(mxValue);
mexPrintf("tf: %i \n", tf);
无论我将 testStruct.tf
设置为 true
还是 false
,它都会打印 tf: 1
。我还使用 if 条件对其进行了测试,无论我输入什么逻辑,if 条件都会执行。
我试过 bool tf = mxGetLogicals(mxValue)
,但没用。
我能得到这方面的指导吗?
Can I get a pointer on this?
... 这就是问题所在... mxGetLogical
returns 指向 mxArray 中第一个逻辑元素的指针。 see documentation.
所以试试这个(编译为 mexTest):
#include "mex.h"
void mexFunction( int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[] )
{
mxArray *mxValue;
mxLogical *tf;
mxValue = mxGetField(prhs[0], 0, "tf");
tf = mxGetLogicals(mxValue);
mexPrintf("tf: %i \n", *tf);
}
运行 它给了我这些结果:
>> testStruct.tf = true;
>> mexTest(testStruct)
tf: 1
>> testStruct.tf = false;
>> mexTest(testStruct)
tf: 0