C/C++调用Matlab

C/C++调用Matlab

基本说明

  • 环境:Win10、 Matlab2016b、Vs2015、编译器 Microsoft Visual C++ 2015 Professional
  • C/C++调用Matlab需要用到两个不同的API C 矩阵 API用于 C++ 的 MATLAB 数据 API
  • 同时可以使用MatlabMex 编译或者使用IDE(这里使用VS2015)配置编译
  • 这种方法应该属于混合编程的一种 可以在C/C++编程过程中使用Matlab的函数

C 调用Matlab

Matlab构建程序
  • 使用MatlabMex编译代码
% 复制 engwindemo.c
copyfile(fullfile(matlabroot,'extern','examples','eng_mat','engwindemo.c'),'.','f')
% 设置编译器
mex -setup -client engine C
% 编译文件
mex -v -client engine engwindemo.c
VS2015 IDE配置
  • C++为例 新建一个VS项目拷贝engdemo.cpp的代码
/*
*	engdemo.cpp
*
*	A simple program to illustrate how to call MATLAB
*	Engine functions from a C++ program.
*
* Copyright 1984-2011 The MathWorks, Inc.
* All rights reserved
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "engine.h"
#define  BUFSIZE 256

int main()

{
	Engine *ep;
	mxArray *T = NULL, *result = NULL;
	char buffer[BUFSIZE + 1];
	double time[10] = { 0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0 };

	/*
	* Call engOpen with a NULL string. This starts a MATLAB process
	* on the current host using the command "matlab".
	*/
	if (!(ep = engOpen(""))) {
		fprintf(stderr, "\nCan't start MATLAB engine\n");
		return EXIT_FAILURE;
	}

	/*
	* PART I
	*
	* For the first half of this demonstration, we will send data
	* to MATLAB, analyze the data, and plot the result.
	*/

	/*
	* Create a variable for our data
	*/
	T = mxCreateDoubleMatrix(1, 10, mxREAL);
	memcpy((void *)mxGetPr(T), (void *)time, sizeof(time));
	/*
	* Place the variable T into the MATLAB workspace
	*/
	engPutVariable(ep, "T", T);

	/*
	* Evaluate a function of time, distance = (1/2)g.*t.^2
	* (g is the acceleration due to gravity)
	*/
	engEvalString(ep, "D = .5.*(-9.8).*T.^2;");

	/*
	* Plot the result
	*/
	engEvalString(ep, "plot(T,D);");
	engEvalString(ep, "title('Position vs. Time for a falling object');");
	engEvalString(ep, "xlabel('Time (seconds)');");
	engEvalString(ep, "ylabel('Position (meters)');");

	/*
	* use fgetc() to make sure that we pause long enough to be
	* able to see the plot
	*/
	printf("Hit return to continue\n\n");
	fgetc(stdin);
	/*
	* We're done for Part I! Free memory, close MATLAB figure.
	*/
	printf("Done for Part I.\n");
	mxDestroyArray(T);
	engEvalString(ep, "close;");


	/*
	* PART II
	*
	* For the second half of this demonstration, we will request
	* a MATLAB string, which should define a variable X.  MATLAB
	* will evaluate the string and create the variable.  We
	* will then recover the variable, and determine its type.
	*/

	/*
	* Use engOutputBuffer to capture MATLAB output, so we can
	* echo it back.  Ensure first that the buffer is always NULL
	* terminated.
	*/

	buffer[BUFSIZE] = '\0';
	engOutputBuffer(ep, buffer, BUFSIZE);
	while (result == NULL) {
		char str[BUFSIZE + 1];
		/*
		* Get a string input from the user
		*/
		printf("Enter a MATLAB command to evaluate.  This command should\n");
		printf("create a variable X.  This program will then determine\n");
		printf("what kind of variable you created.\n");
		printf("For example: X = 1:5\n");
		printf(">> ");

		fgets(str, BUFSIZE, stdin);

		/*
		* Evaluate input with engEvalString
		*/
		engEvalString(ep, str);

		/*
		* Echo the output from the command.
		*/
		printf("%s", buffer);

		/*
		* Get result of computation
		*/
		printf("\nRetrieving X...\n");
		if ((result = engGetVariable(ep, "X")) == NULL)
			printf("Oops! You didn't create a variable X.\n\n");
		else {
			printf("X is class %s\t\n", mxGetClassName(result));
		}
	}

	/*
	* We're done! Free memory, close MATLAB engine and exit.
	*/
	printf("Done!\n");
	mxDestroyArray(result);
	engClose(ep);

	return EXIT_SUCCESS;
}

  • 下面的路径换成自己的路径

  • 右键项目->属性 ->C/C+±>常规->附加包含目录->D:\Program Files\MATLAB\R2016b\extern\include->确定
    在这里插入图片描述

  • 右键项目->属性 ->链接器->常规->附加库目录->D:\Program Files\MATLAB\R2016b\extern\lib\win64\microsoft->确定
    在这里插入图片描述

  • 右键项目->属性 ->链接器 ->输入 ->附加依赖项-> libeng.lib;libmx.lib;libmex.lib;libmat.lib->确定
    在这里插入图片描述

  • 后面再学习函数和语法

参考

从 C 调用 MATLAB

从 C 应用程序中调用 MATLAB 函数

使用 IDE 编译引擎应用程序

Build Windows Engine Application

C++ 调用Matlab

  • C++调用MatlabAPI需要Matlab2018a及以后的版本才提供
参考

从 C++ 调用 MATLAB

从 C++ 调用 MATLAB 函数

用于 C++ 的 MATLAB 数据 API

  • 0
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值