使用Matlab"engine.h";正确地从c ++ [英] Using Matlab "engine.h" from c++ correctly

查看:391
本文介绍了使用Matlab"engine.h";正确地从c ++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a code that proceses frames in each iteration and generatesa matrix. My final goal is to send the matrix data to matlab in order to examine the evolution of the matrix with each frame. In order to achieve this I defined a static variable Engine in a header file (helper.h).

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

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

In the main() program I open the engine only once:

#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 Engine的矩阵的位置,所以我要执行以下操作:

And inside matrix.cpp is where I get the matrix I want to send to Matlab Engine, so I do something like this:

#include helper.h

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

我想使用指针来最有效地引擎.我对如何正确使用Matlab引擎感到困惑.

I want to use the pointer to engine the most efficient way. I am a bit conffused about how to correctly use matlab engine.

任何帮助都将受到欢迎,因为matlab文档和示例完全没有帮助,因为它们在同一文件中拥有所有代码,并且不使用迭代.预先感谢.

Any help woould be welcomed, because the matlab documentation and examples didn't help at all as they have all the code in the same file and they don't use iterations. Thanks in advance.

编辑

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

First problem solved about the engine pointer. The solution is declaring it as extern.

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

和main.cpp

#include helper.h   
Engine *engine=NULL;

main(){}

推荐答案

static的意思是当前编译单元本地".编译单元通常是单个.cpp文件,因此程序中有两个engine变量,一个在main.o中,一个在matrix.o中.您需要在头文件中将engine声明为extern,并在一个.cpp文件中完全没有任何修饰符的情况下对其进行定义.

static means "local to the current compilation unit". A compilation unit is normally a single .cpp file, so you have two engine variables in your program, one in main.o and one in matrix.o. You need to declare engine as extern in the header file and define it without any modificator in exactly one .cpp file.

helper.h:

extern Engine* engine;

main.cpp:

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

这篇关于使用Matlab"engine.h";正确地从c ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆