如何从C调用Matlab的功能++ [英] How to Call Matlab Functions from C++

查看:203
本文介绍了如何从C调用Matlab的功能++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要调用MATLAB函数在我的C ++项目。

I want to call MATLAB function in my C++ project.

我用Matlab R2010a版本和Visual Studio 2010

I'm using Matlab R2010a and Visual Studio 2010

首先,我创建了一个简单的MATLAB功能:

First I created a simple matlab function:

function y = foo(x)
    y = x+1;

然后我用MATLAB编译器使用MATLAB GUI编译器来编译这个功能(文件 - >新建 - >部署项目,然后选择C ++共享库)。它产生这个文件的2个文件夹:DISTRIB和src

and then I used matlab compiler to compile this function using matlab GUI compiler (File-> new -> Deployment Project and then choose C++ shared Library). It produces this files 2 folders: distrib and src.

DISTRIB包括:

distrib contains:


  1. foo.dll

  2. foo.h中

  3. foo.lib

src包含:


  1. Foo.cpp中

  2. foo.dll

  3. foo.exp

  4. foo.exports

  5. foo.h中

  6. foo.lib

  7. foo_mcc_component_data.c

我想使用此文件在C ++应用程序。我试过很多次,我没有找到一个方法。我发现在互联网上的所有方法都使用旧MATLAB编译器产生不同的文件或者工作在旧版本的Visual Studio。

I want to use this file in a C++ application. I tried many times and I didn't find a way. All the ways I found over the internet are using old matlab compiler which produces different files or works on an old version of visual studio.

所以,请任何人都可以帮我吗?

So please could anyone help me?

我应该做些什么?我必须添加哪些文件/引用,并到哪里呢?我必须定义什么路径?

What must I do? What files/references must I add and to where? What paths must I define?

推荐答案

也许为时已晚,但对未来的。

maybe it is too late but for the future.

包含 foo.h中

添加的 C / C ++ - 常规 - 附加包含目录的方式,以MATLAB的头文件( C:\\ Program Files文件(x86)的\\ MATLAB \\ R2009b中的\\ EXTERN \\包含)。

Add C/C++-General-Additional Include Directories the way to the matlab headers (C:\Program Files (x86)\MATLAB\R2009b\extern\include).

添加 foo.lib mclmcrrt.lib mclcommain.lib 在附加依赖链接器的。

有关的链接的中的附加库目录的显示方式,您的MATLAB库( C:\\ Program Files文件(x86)的\\ MATLAB \\ R2009b中的\\ EXTERN \\ LIB \\ WIN32 \\微软 32位的版本(MATLAB和VS的版本应该是一样的。我必须安装第二Matlab的32位版本))。

For linker in Additional Library Directories show the way to your matlab libs(C:\Program Files (x86)\MATLAB\R2009b\extern\lib\win32\microsoft for 32bit ver (matlab and VS versions should be the same. I had to install the second Matlab 32bit version.)).

我在系统路径中添加的方式向 foo.lib

I added the way to the foo.lib in my system path.

使用库之前 foo.dll ,你应该初始化MCR和库函数。

Before using your library foo.dll, you should initialize MCR and library function.

mclInitializeApplication(NULL,0);
fooInitialize(); 

使用不要忘后:

mclTerminateApplication();
fooTerminate();

和一些示范code,是这样的:

And some demonstration code, looks like:

int num = 1;
double numbrIn = 1.5;
std::cout<<"now we have " << numbrIn << std::endl;
mwArray array_in(num, 1, mxDOUBLE_CLASS, mxREAL);
array_in.SetData(&numbrIn,num);
mwArray array_out;
foo(1, array_out, array_in);
array_out.GetData(&numbrIn, num);
std::cout<<"now we have " << numbrIn << std::endl;

这篇关于如何从C调用Matlab的功能++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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