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++;到mex文件:系统命令的输出被抑制 我写了一个C++代码,我把它转换成MEX文件,这样我就可以从MATLAB中运行。我的C++代码显示了第三方库中声明的函数输出。但是,当我将其转换为mex文件时,输出似乎被抑制_C++_Matlab_Mex - Fatal编程技术网

C++;到mex文件:系统命令的输出被抑制 我写了一个C++代码,我把它转换成MEX文件,这样我就可以从MATLAB中运行。我的C++代码显示了第三方库中声明的函数输出。但是,当我将其转换为mex文件时,输出似乎被抑制

C++;到mex文件:系统命令的输出被抑制 我写了一个C++代码,我把它转换成MEX文件,这样我就可以从MATLAB中运行。我的C++代码显示了第三方库中声明的函数输出。但是,当我将其转换为mex文件时,输出似乎被抑制,c++,matlab,mex,C++,Matlab,Mex,注意:以下命令的输出被抑制 int systemRet = std::system("./genb_test"); 原始代码: #include <stdio.h> /* defines FILENAME_MAX */ #include <cstdlib> #include <iostream> #ifdef _MSC_VER #include "direct.h" #define GetCurrentDir _getcwd

注意:以下命令的输出被抑制

 int systemRet = std::system("./genb_test");
原始代码:

 #include <stdio.h>  /* defines FILENAME_MAX */
 #include <cstdlib>

 #include <iostream>
 #ifdef _MSC_VER
    #include "direct.h" 
    #define GetCurrentDir _getcwd // window ??
 #else
    #include "unistd.h"
        #define GetCurrentDir getcwd
 #endif


int main()
{
  const char *ParentFolder = "/home/dkumar/libtsnnls-2.3.3/tsnnls/";

  int res3 = chdir(ParentFolder);
  if (res3  == -1){
    // The system method failed
    std::cout<< "the chdir method has failed \n"; 
  }    

  char cCurrentPath[FILENAME_MAX];
  if (!GetCurrentDir(cCurrentPath, sizeof(cCurrentPath)))
  {
      printf("Could not find current directory " );
      // return errno;
  }
  cCurrentPath[sizeof(cCurrentPath) - 1] = '\0'; /* not really required */
  printf ("The current working directory is %s", cCurrentPath);
  printf ("\n");

  printf("Now running genb test " );
  int systemRet = std::system("./genb_test");
  if(systemRet == -1){
    // The system method failed
  }else{
    printf("System command execuated successfully " );
  }

return 0;
}
The current working directory is /home/dkumar/libtsnnls-2.3.3/tsnnls
genb_tests

Creating 100 random test 121 x 89 problems using 
the genb algorithm of PJV. Each problem will be given 
to the tsnnls method, and the error printed below.
We require an error less than 1e-8 to pass the test.

#    M    N     Error         (PJV error)   (Spiv error)   Result 
----------------------------------------------------------------- 
  1  121  89    1.375271e-15  1.375271e-15  0.000000e+00  pass
  2  121  89    1.953126e-15  1.953126e-15  0.000000e+00  pass
  3  121  89    4.272569e-15  4.272569e-15  0.000000e+00  pass
  4  121  89    1.440234e-15  1.440234e-15  0.000000e+00  pass
  5  121  89    2.392671e-15  2.392671e-15  0.000000e+00  pass
 .......
 ....... 

 98  121  89    4.696796e-15  4.696796e-15  0.000000e+00  pass
 99  121  89    1.820247e-15  1.820247e-15  0.000000e+00  pass
100  121  89    1.520109e-15  1.520109e-15  0.000000e+00  pass

100 (of 100) tests passed.
Now running genb test System command execuated successfully
将原始代码翻译成mex文件:各种输入和输出(LHS)保持原样,因为我很快就会开始使用它

#include <matrix.h> // mex
#include <mex.h>    // mex

#include <iostream>  // Basic I/O
using namespace std; // Basic I/O


/* Definitions to keep compatibility with earlier versions of ML */
#ifndef MWSIZE_MAX
typedef int mwSize;
typedef int mwIndex;
typedef int mwSignedIndex;

#if (defined(_LP64) || defined(_WIN64)) && !defined(MX_COMPAT_32)
/* Currently 2^48 based on hardware limitations */
# define MWSIZE_MAX    281474976710655UL
# define MWINDEX_MAX   281474976710655UL
# define MWSINDEX_MAX  281474976710655L
# define MWSINDEX_MIN -281474976710655L
#else
# define MWSIZE_MAX    2147483647UL
# define MWINDEX_MAX   2147483647UL
# define MWSINDEX_MAX  2147483647L
# define MWSINDEX_MIN -2147483647L
#endif
#define MWSIZE_MIN    0UL
#define MWINDEX_MIN   0UL
#endif

// 'Hello World!' program 
 #include <stdio.h>  /* defines FILENAME_MAX */
 #include <cstdlib>

 #include <iostream>
 #ifdef _MSC_VER
    #include "direct.h" 
    #define GetCurrentDir _getcwd // window ??
 #else
    #include "unistd.h"
        #define GetCurrentDir getcwd
 #endif


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

  //const char *ParentFolder = "/home/dkumar/All_Matlab_Codes_DKU/";
  const char *ParentFolder = "/home/dkumar/libtsnnls-2.3.3/tsnnls/";


//declare variables
    mxArray *a_in_m, *b_in_m, *c_out_m, *d_out_m;
    const mwSize *dims;
    double *a, *b, *c, *d;
    int dimx, dimy, numdims;
    int i,j;

//associate inputs
    a_in_m = mxDuplicateArray(prhs[0]);
    b_in_m = mxDuplicateArray(prhs[1]);

//figure out dimensions
    dims = mxGetDimensions(prhs[0]);
    numdims = mxGetNumberOfDimensions(prhs[0]);
    dimy = (int)dims[0]; dimx = (int)dims[1];

//associate outputs
    c_out_m = plhs[0] = mxCreateDoubleMatrix(dimy,dimx,mxREAL);
    d_out_m = plhs[1] = mxCreateDoubleMatrix(dimy,dimx,mxREAL);

//associate pointers
    a = mxGetPr(a_in_m);
    b = mxGetPr(b_in_m);
    c = mxGetPr(c_out_m);
    d = mxGetPr(d_out_m);


  std::cout<< "Trying to change the directory "<< "\n";

  // COPIED FROM ORIGINAL C++ 
  int res3 = chdir(ParentFolder);
  if (res3  == -1){
    // The system method failed
    std::cout<< "the chdir method has failed \n"; 
  }

  char cCurrentPath[FILENAME_MAX];
  if (!GetCurrentDir(cCurrentPath, sizeof(cCurrentPath)))
  {
      printf("Could not find current directory " );
      // return errno;
  }
  cCurrentPath[sizeof(cCurrentPath) - 1] = '\0'; /* not really required */
  printf ("The current working directory is %s", cCurrentPath);
  printf ("\n");

  printf("Now running genb test " );
  int systemRet = std::system("./genb_test");
  if(systemRet == -1){
    // The system method failed
  }else{
    printf("System command execuated successfully " );
  }

   // ADDED THIS PART at the suggestion of king_nak
   //Capturing the output terminal
    FILE * f = popen( "ls -al", "r" );
    if ( f == 0 ) {
        fprintf( stderr, "Could not execute\n" );
        return;
    }
    const int BUFSIZE = 1000;
    char buf[ BUFSIZE ];
    while( fgets( buf, BUFSIZE,  f ) ) {
        fprintf( stdout, "%s", buf  );
    }
    pclose( f );

    return;
}
我们将非常感谢您的帮助

问候,,
Dushyant

您是否尝试过mex命令
mexPrintf

但是,在执行整个mex程序之前,不会打印输出。这有两种解决方法,一种是使用

mexEvalString("disp('Bla')")


std::system
将启动系统的命令处理器执行命令。如果您有一个控制台应用程序,这将打印输出到当前控制台。这就是为什么您在测试程序中看到它。输出未传递给调用程序

在您的例子中,Matlab似乎在后台启动该过程,在后台输出被丢弃。尝试打开进程并将其输出读取到程序/MEX中

在POSIX中,您可以使用
popen
(参见示例),在Windows中,您可以使用
ReadPipe
(参见)

更新
你必须调整我链接到的代码。原始代码调用
ls-al
,并将其输出打印到屏幕上。您必须调用您的进程
genb_test

使用此代码在matlab中获取输出,而不是调用
std::system

FILE * f = popen( "genb_test", "r" );  // <- call genb_test
if ( f == 0 ) {
    fprintf( stderr, "Could not execute\n" );
    return;
}
const int BUFSIZE = 1000;
char buf[ BUFSIZE ];
while( fgets( buf, BUFSIZE,  f ) ) {
    mexPrintf(buf); // <- use mexPrintf to print to matlab
}
pclose( f );

FILE*f=popen(“genb_测试”,“r”);//调用mexEvalString(“./genb_test”)或mexEvalString(“system(“./genb_test”)”)会给我带来错误。你能详细说明一下吗?我的理解是MEX文件应该复制我在C++代码中所做的事情。试试<代码> MyValuScript(“DISP(……)))< /C>code>mexEvalString
在mex程序中调用matlab函数,“/genb_test”不是matlab函数。感谢您的澄清/genb_测试是gcc可执行的。这意味着您的解决方案在这种情况下无效。谢谢。我可以在启动matlab的终端看到输出。我也试过使用popen(请看我修改过的代码),它似乎也在向终端发送输出,或者,我可能没有正确使用。但是,我想使用system捕获命令的输出:c=run_some_test(inputs…);换句话说,我希望c的值在mex文件中可用。请帮忙。我用一个例子扩展了答案。您必须调用您的进程,而不是ls-al!
 mexPrintf("Bla")
 mexEvalString("drawnow;");
FILE * f = popen( "genb_test", "r" );  // <- call genb_test
if ( f == 0 ) {
    fprintf( stderr, "Could not execute\n" );
    return;
}
const int BUFSIZE = 1000;
char buf[ BUFSIZE ];
while( fgets( buf, BUFSIZE,  f ) ) {
    mexPrintf(buf); // <- use mexPrintf to print to matlab
}
pclose( f );