How to call Matlab function in C++
来源:互联网 发布:如何建立网络共享 编辑:程序博客网 时间:2024/04/28 20:13
How to call Matlab function in C++
In order to make more people get benefits, I decide to write this tutorial in English. This tutorial displays how to call Matlab libraries in C++ in Windows platform.
Prerequisites:
1.Windows operating system, I test my code on Windows 7 Enterprise SP1 x64 and Windows 8 Pro x64.
2.Matlab, I test my code in Matlab 2010a, and newer versions are also compatible, I think.
3.Microsoft Visual Studio, I test my code on VS 2010 ultimate and VS 2012 ultimate. Express Suites maybe work well.
Step 1:
Set Path variable in Advanced system settings-> Environment Variables: C:\Program Files\MATLAB\R2010a\bin\win64 (or the path where libeng.dll is located)
Step 2:
In project properties of Visual Studio, add Additional Include directories of C++ -> General: "C:\Program Files\MATLAB\R2010a\extern\include"
Step 3:
In project properties of Visual Studio, linker->general, Additional Library Directories: "C:\Program Files\MATLAB\R2010a\extern\lib\win64\microsoft"
In project properties of Visual Studio, linker->input, Additional Dependencies: libeng.lib libmx.lib
Step 4:
Change the Active Solution Platform to x64 or x86 which depends on your Matlab runtime version.
Demo code:
The demo code will show how to create a sparse matrix, then put it into Matlab workspace.
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
namespace
MATLAB
{
// you can use namespace to avoid name conflicts
#include "engine.h"
}
int
main()
{
double
data[4] = {5.8, 6.2, 5.9, 6.1};
size_t
row[4] = {0, 2, 1, 3};
size_t
col[3] = {0, 2, 4};
static
MATLAB::Engine *ep = MATLAB::engOpen(
"\0"
);
if
(!ep)
{
fprintf
(stderr,
"\nCan't start MATLAB engine\n"
);
return
-1;
}
MATLAB::mxArray* M = MATLAB::mxCreateSparse(4, 2, 4, MATLAB::mxREAL);
double
* pv = (
double
*)MATLAB::mxGetPr(M);
memcpy
(pv, data, 4 *
sizeof
(
double
));
size_t
* pi = (
size_t
*)MATLAB::mxGetIr(M);
memcpy
(pi, row, 4*
sizeof
(
size_t
));
size_t
* pj = (
size_t
*)MATLAB::mxGetJc(M);
memcpy
(pj, col, 3*
sizeof
(
size_t
));
MATLAB::engPutVariable(ep,
"M"
, M);
MATLAB::mxDestroyArray(M);
MATLAB::engEvalString(ep,
"M = M'"
);
MATLAB::engEvalString(ep,
"M = full(M)"
);
MATLAB::mxArray* result;
double
res[8];
if
((result = MATLAB::engGetVariable(ep,
"M"
)) == NULL)
{
printf
(
"Oops! You didn't create a variable X.\n\n"
);
}
memcpy
((
void
*)res, (
void
*)MATLAB::mxGetPr(result),
sizeof
(res));
for
(
int
i = 0 ;i < 2 ;i ++)
{
for
(
int
j = 0; j < 4; j ++)
{
printf
(
"%lf "
,res[j * 2 + i]);
}
printf
(
"\n"
);
}
MATLAB::engClose(ep);
return
0;
}
- How to call Matlab function in C++
- how to call c++ function in python coding in linux?
- How to call a service function in AngularJS ng-click
- How to create function polymorphism in C
- How to call an external C function from within Oracle
- lua study -- how to call lua function use C language
- How to fix Fatal error: Call to undefined function: mysql_connect() in Ubuntu
- How to call function from register view
- Call to undefined function imagecreatefromjpeg() in
- How to Call C# DLL in Delphi7
- How to Call Back In Com ?
- How to call API in Java
- How to call stored procedure in Hibernate
- How to call stored procedure in Hibernate
- How to create Function/Procedure in Oracle?
- How to create a function in SQL
- How do I call a C++ function from C?
- how to install Matlab API in caffe
- 几个小算法练习
- Linux 查看corefile文件所在路径
- ExtJS4.1+MVC3+Spring.NET1.3+EF5 整合五:数据访问层
- java零碎要点---1.JAVA邮件发送的简单实现
- 2012年图灵奖获得者及他们的主要贡献
- How to call Matlab function in C++
- 我的DIY Android之旅--Android2.3源码初识
- 分享下.NET程序读取二代身份证(附源码
- Windows7下硬盘安装Mac OS X Lion(狮子)10.7正式版
- smackx jingle语音通话实例
- 理解面向对象
- 复选框勾选全部实现方法
- JavaScript验证大全
- div和table 用作布局的 区别