【问题标题】:Using Matlab "engine.h" from c++ correctly正确使用 C++ 中的 Matlab“engine.h”
【发布时间】:2012-01-18 20:24:59
【问题描述】:

我有一个代码在每次迭代中处理帧并生成一个矩阵。我的最终目标是将矩阵数据发送到 matlab,以便检查每一帧矩阵的演变。 为了实现这一点,我在头文件 (helper.h) 中定义了一个静态变量 Engine。

#include "engine.h";
#include "mex.h";
static Engine *engine;

在 main() 程序中,我只打开一次引擎:

#include helper.h   


main(){
if (!(engine = engOpen(NULL))) {
    MessageBox ((HWND)NULL, (LPSTR)"Can't start MATLAB engine",(LPSTR) "pcTest.cpp", MB_OK);
    exit(-1);}

//here comes frame processing using a while loop
.
.  //a function is called (defined in matrix.cpp)
.
//frame processing ends
}

在 matrix.cpp 里面是我想要发送到 Matlab 引擎的矩阵,所以我做了这样的事情:

#include helper.h

mxArray *mat;   
mat = mxCreateDoubleMatrix(13, 13, mxREAL);     
memcpy(mxGetPr(mat),matrix.data, 13*13*sizeof(double));
engPutVariable(engine, "mat", mat);

我想以最有效的方式使用指针引擎。我对如何正确使用matlab引擎有点困惑。

欢迎任何帮助,因为 matlab 文档和示例根本没有帮助,因为它们将所有代码都放在同一个文件中,而且它们不使用迭代。提前致谢。

编辑

解决了关于引擎指针的第一个问题。解决方案是将其声明为 extern。

#include "engine.h";
#include "mex.h";
extern Engine *engine;

在 main.cpp 中

#include helper.h   
Engine *engine=NULL;

main(){}

【问题讨论】:

标签: c++ matlab matlab-engine


【解决方案1】:

static 表示“当前编译单元的本地”。一个编译单元通常是一个.cpp 文件,因此您的程序中有两个engine 变量,一个在main.o 中,一个在matrix.o 中。您需要在头文件中将engine 声明为extern,并在一个.cpp 文件中定义它而无需任何修饰符。

helper.h:

extern Engine* engine;

main.cpp:

#include "helper.h"
Engine* engine = NULL;

【讨论】:

  • 我也试过extern,但还是不行。我认为问题在于我将引擎称为我的引擎,并且在“engine.h”内部有一个叫做引擎的东西。我将重命名指针名称。
  • 不,你是对的!在 main.cpp 中执行 Engine* engine = NULL 现在它可以工作了!谢谢。第一个问题解决了!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-03-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-10-09
  • 1970-01-01
相关资源
最近更新 更多