C MATLAB Mex:在MATLAB中从结构中检索逻辑

C MATLAB Mex:在MATLAB中从结构中检索逻辑,c,matlab,struct,mex,C,Matlab,Struct,Mex,我以这种方式创建了一个结构: testStruct=struct;testStruct.tf=true 我想通过Max把这个结构传递到C++代码中,这是我做的一个快照: mxArray *mxValue; mxValue = mxGetField(prhs[0], 0, "tf"); mxLogical tf = mxGetLogicals(mxValue); mexPrintf("tf: %i \n", tf); 无论我将testStruct.tf设置为true还是false,它都会打印t

我以这种方式创建了一个结构:

testStruct=struct;testStruct.tf=true

我想通过Max把这个结构传递到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)
,但没有用

能给我一个指针吗

能给我一个指针吗

。。。这就是问题所在
mxGetLogical
返回指向mxArray中第一个逻辑元素的指针

因此,请尝试以下方法(编译为mexTest):

运行它会给我以下结果:

>> testStruct.tf = true;
>> mexTest(testStruct)
tf: 1 
>> testStruct.tf = false;
>> mexTest(testStruct)
tf: 0 
能给我一个指针吗

。。。这就是问题所在
mxGetLogical
返回指向mxArray中第一个逻辑元素的指针

因此,请尝试以下方法(编译为mexTest):

运行它会给我以下结果:

>> testStruct.tf = true;
>> mexTest(testStruct)
tf: 1 
>> testStruct.tf = false;
>> mexTest(testStruct)
tf: 0