将变量从C ++传递到Matlab(工作空间) [英] Passing Variable from C++ to Matlab (Workspace)

查看:865
本文介绍了将变量从C ++传递到Matlab(工作空间)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图传递一个变量 z = 100 从C ++到Matlab进一步处理(这只是一个非常简化的例子)。我基本上希望这可以作为一个全局变量传递,这样我可以从任何Matlab函数访问这个变量(也许发送到Matlab工作区)。



这是我的C ++代码(我在C ++中使用Matlab引擎):

  #includeopencv2 / highgui / highgui.hpp 
#includeopencv2 / opencv.hpp
#include< iostream>
#include< math.h>
#include< fstream>
#include< stdio.h>
#include< Windows.h>
#includeengine.h
#include< engine.h>
//#include< cmath>
#define _USE_MATH_DEFINES

#pragma comment(lib,libmat.lib)
#pragma comment(lib,libmx.lib)
#pragma comment (lib,libmex.lib)
#pragma comment(lib,libeng.lib)

using namespace cv;
using namespace std;

int main(int argc,char * argv [])
{
Engine * ep = engOpen(NULL);
int z;
mxArray * z_array = mxCreateDoubleMatrix(1,1,mxREAL);
double * pz = mxGetPr(z_array);
z = 100;
engPutVariable(ep,z,z_array);

engClose(ep);

return 0;
}

当这段代码执行完毕后,我打开Matlab并尝试访问变量 z ,但它不存在。有没有什么我在这里失踪? (我也试过在后插入 engEvalString(ep,global z; disp(z);); engPutVariable



=h2_lin>解决方案

您的代码对我看起来不错
通过writng engClose(ep);关闭Matlab引擎,使得变量z将随着Matlab会话消失。 p>

编辑:通过检查你的代码,我注意到z没有受到z_array的影响,所以试试下面的代码

  mxArray * z_array = NULL; 
double z [1] = {100};
z_array = mxCreateDoubleMatrix(1,1,mxREAL);
memcpy (b * b)
(b * b)
(b * b)


I'm trying to pass a variable z = 100 from C++ to Matlab for further processing (this is just a very simplified example). I basically want this to be passed as a global variable such that I can access this variable from any Matlab function, (perhaps sent to the Matlab Workspace).

Here is my C++ code (I'm using the Matlab engine from within C++):

#include "opencv2/highgui/highgui.hpp"
#include "opencv2/opencv.hpp"
#include <iostream>
#include <math.h>
#include <fstream>
#include <stdio.h>
#include <Windows.h>
#include "engine.h"
#include <engine.h>
//#include <cmath>
#define _USE_MATH_DEFINES

#pragma comment ( lib, "libmat.lib" )
#pragma comment ( lib, "libmx.lib" )
#pragma comment ( lib, "libmex.lib" )
#pragma comment ( lib, "libeng.lib" )

using namespace cv;
using namespace std;

int main (int argc, char* argv[])
{
    Engine *ep = engOpen(NULL);
    int z;
    mxArray *z_array = mxCreateDoubleMatrix(1,1,mxREAL);
    double *pz = mxGetPr(z_array);
    z = 100;
    engPutVariable(ep, "z", z_array);

    engClose(ep);

    return 0;
}

When this code has finished executing, I open Matlab and try to access the variable z but it doesn't exist. Is there something I'm missing here? (I've also tried inserted engEvalString(ep, "global z; disp(z);"); after engPutVariable but this doesn't help.

I'd appreciate any insight you might be able to give me. Thanks!

解决方案

Your code looks good to me. By writng engClose(ep); you close the Matlab Engine so the variable z will disappear with the Matlab session.

EDIT : By reviewing your code, I noticed that z has not been affected to z_array. So try the following code

mxArray *z_array = NULL;
double z[1] = {100};
z_array = mxCreateDoubleMatrix(1, 1, mxREAL);
memcpy((char *) mxGetPr(z_array), (char*) z, sizeof(double));
engPutVariable(ep, "z", z_array);
mxDestroyArray(z_array);

这篇关于将变量从C ++传递到Matlab(工作空间)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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