C调用MATLAB_engine进行混编的两个环境:win7和Linux

完整代码见https://github.com/fyang21117/CMATLAB

---------------------------------------------------------------------------------------------

一、Windows环境(win7,VS2017+MATLAB_R2016b_x64)

1、必要环境设置

链接文件:libmx.lib,libmex.lib和libmat.lib (位于MATLAB库目录);

engine.h,matrix.h,tmwtypes.h(位于MATLAB包含目录)

包含目录:D:\Program Files (x86)\SetupDir\MATLAB\R2016b\extern\include;

库目录:

D:\ProgramFiles (x86)\SetupDir\MATLAB\R2016b\extern\lib\win64\microsoft;

D:\ProgramFiles (x86)\SetupDir\MATLAB\R2016b\extern\lib\win64\mingw64;

附加器目录: D:\ProgramFiles (x86)\SetupDir\MATLAB\R2016b\bin\win64; (可不加)

二、Linux环境(Centos7.5,gcc-7.3.0+matlab_R2016b_x64)

1、必要环境设置(matlab默认安装情况下)

链接文件:libmx.so,libmex.so和libmat.so

头文件:engine.h,matrix.h,tmwtypes.h

包含目录:/usr/local/MATLAB/R2016b/extern/include

库目录:/usr/local/MATLAB/R2016b/bin/glnxa64

2、 matlab引擎依赖条件

  ①matlab引擎依赖/bin/csh启动,所以不管你使用何种shell,都必须安装csh

  #yum install chs    (centos/redhat平台)

②gcc版本过低,会出现链接库的问题(经测试,6.1.0版本gcc也是可以的)


三、示例代码

#include 
#include 
#include 
#include "engine.h"
#include "matrix.h"
#define  BUFSIZE 256
#define  N 120
int main()
{
	Engine *ep;										// define a pointer of Engine
	char level,buffer[BUFSIZE + 1];
	int i=25,num;
	mxArray *testdata = NULL, *result = NULL;
	double cArray[N] = { 
		0	,	4	,	10	,	1	,1	,	7	,	15	,	1	,1	,	11	,	12	,	1	,
		1	,	23	,	14	,	1	,1	,	25	,	18	,	1	,1	,	26	,	14	,	1	,
		1	,	39	,	26	,	1	,1	,	36	,	36	,	1	,1	,	26	,	42	,	1	,
		1	,	39	,	36	,	1	,1	,	38	,	31	,	2	,1	,	36	,	35	,	2	,
		1	,	36	,	35	,	2	,1	,	48	,	31	,	1	,1	,	39	,	22	,	1	,
		1	,	17	,	11	,	1	,1	,	33	,	13	,	1	,2	,	65	,	9	,	1	,
		2	,	50	,	17	,	1	,2	,	74	,	10	,	1	,1	,	51	,	44	,	1	,
		1	,	28	,	62	,	1	,1	,	17	,	37	,	1	,1	,	7	,	21	,	2	,
		1	,	6	,	12	,	1	,1	,	24	,	12	,	1	,1	,	39	,	16	,	1	,
		2	,	49	,	13	,	1	,1	,	29	,	30	,	1	,1	,	24	,	33	,	2
	};
        //-----windows下将engOpen的参数改为---engOpen(NULL) ...-------------------------
	if (!(ep = engOpen("/usr/local/MATLAB/R2016b/bin/matlab")))
	{
		printf("Can't start MATLAB engine!");
		return EXIT_FAILURE;
	}
	engSetVisible(ep,false);
	testdata = mxCreateDoubleMatrix(1, N, mxREAL);										

	memcpy((double *)mxGetPr(testdata), (double *)cArray, N * sizeof(double));

        //Put testdata into Matlab workspace,and name it with input_data

        int flag = engPutVariable(ep, "input_data", testdata);

                 if(flag==1)

		    printf("Failed to send  testdata to MATLAB !");				
	engEvalString(ep, "save data.mat 'input_data';");
	buffer[BUFSIZE] = '\0';
	engOutputBuffer(ep, buffer, BUFSIZE);			//result buffer
	engEvalString(ep, "MTest");			                //run MethodTest.m
	engOutputBuffer(ep, NULL, 0);					

	if ((result = engGetVariable(ep, "num")) == NULL)
	{
		printf("The  result of MATLAB is %s.\n", buffer);								
		for(;!(buffer[i]>='A' && buffer[i]<='Z');i++)
		{
		  if(buffer[i]>='0' && buffer[i]<='9')
			return_num=(int)(buffer[i]-48);
		}
		level=(char)buffer[i];
		printf("The return_num is %d\n\n",num);	//num and level are results of running m file 
		printf("The level is %c\n\n",level);		                
	}
	else
	{
		 printf("There is something wrong with MTest.m!Check again!\n" );
	}
	mxDestroyArray(testdata);				                //free the memory
	mxDestroyArray(result);										
	engClose(ep);						                        //close MATLAB Engine
	return EXIT_SUCCESS;
}

本示例代码实现的功能:在c语言里面往MATLAB传送一组数据,并传输指令调用MTest.m进行处理,得到返回结果num和level。

更多链接:

https://ww2.mathworks.cn/help/matlab/matlab-c-api-to-read-mat-file-data.html;jsessionid=b47c81b204f996c1734216829d51?nocookie=true&ue





你可能感兴趣的:(C调用MATLAB_engine进行混编的两个环境:win7和Linux)