Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/matlab/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ MATLAB中MEX函数的逻辑输出问题_C++_Matlab_Mex - Fatal编程技术网

C++ MATLAB中MEX函数的逻辑输出问题

C++ MATLAB中MEX函数的逻辑输出问题,c++,matlab,mex,C++,Matlab,Mex,为什么我的MEX函数的输出总是1,尽管它应该是0 我编写了以下MEX源代码,如下所示 #include "mex.h" void mexFunction( int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[] ) { bool *x,*y; /* Create matrix for the return argument. */ plhs[0] = mxCreateLogicalMatrix(1,1); /*

为什么我的MEX函数的输出总是1,尽管它应该是0

我编写了以下MEX源代码,如下所示

#include "mex.h"

void mexFunction( int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[] )
{
  bool *x,*y;

  /* Create matrix for the return argument. */
  plhs[0] = mxCreateLogicalMatrix(1,1);

  /* Assign pointers to each input and output. */
  x = mxGetLogicals(prhs[0]); //input
  y = mxGetLogicals(plhs[0]); //output

  /* Calculations. */
  if (*x == 0) *y = 1;
  else *y = 0;
}
出现以下内容:

y = test(5)

y =

     1

我想给你指一下我的文件。文件的一部分说:

返回

指向
mxArray
中第一个逻辑元素的指针。如果
mxArray
不是逻辑数组,则结果为未指定

您传递的是一个
双精度
数字,不是
逻辑
。通过这样做,您将获得未定义的行为。因此,有三种方法可以解决此错误:

  • 将实际
    逻辑
    值传递给函数
  • 让一切保持原样,但改变你要返回的内容。将其分别更改为
    true
    false
    ,而不是
    *y=1
    *y=0
    ,但输入必须是
    双精度
  • 基本上,您必须将对
    logical/bool
    的任何引用更改为
    double
    。具体来说,将
    mxGetLogicals
    更改为,以便可以获得指向
    双精度实数数组的指针。您还需要将
    mxCreateLogicalMatrix
    更改为,并且必须将指针从
    bool
    更改为
    double
  • 选项#1-将
    逻辑值传递给函数:
    您只需执行以下操作:

    y = test(false);
    
    或:


    通过这些更改运行此命令,可以获得以下信息:

    >> y = test(false)
    
    y =
    
         1
    
    >> y = test(true)
    
    y =
    
         0
    
    选项2-输入类型为
    double
    ,输出类型为
    bool
    : 您需要进行以下更改:

    #include "mex.h"
    
    void mexFunction( int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[] )
    {
      double *x;
      bool *y; // Change
    
      /* Create matrix for the return argument. */
      plhs[0] = mxCreateLogicalMatrix(1,1);
    
      /* Assign pointers to each input and output. */
      x = mxGetPr(prhs[0]); //input - Change
      y = mxGetLogicals(plhs[0]); //output
    
      /* Calculations. */
      if (*x == 0) *y = true; // Change
      else *y = false;
    }
    


    使用上述更改运行此代码会使我:

    >> y = test(0)
    
    y =
    
         1
    
    >> y = test(5)
    
    y =
    
         0
    
    >> y = test(0)
    
    y =
    
         1
    
    >> y = test(5)
    
    y =
    
         0
    
    选项3-将
    bool
    行为更改为
    double

    使用上述更改运行此代码会使我:

    >> y = test(0)
    
    y =
    
         1
    
    >> y = test(5)
    
    y =
    
         0
    
    >> y = test(0)
    
    y =
    
         1
    
    >> y = test(5)
    
    y =
    
         0
    

    是否也可以将函数更改为接受双输入但仍输出逻辑?@aschepler是的。我只是没想到。我将在中添加另一个选项。我的答案有帮助吗?是的,非常感谢:)