# MATLAB和VS混合编程

MATLAB R2014a，VS2013;

function [sum,prod]=sum_prod(p1,p2)
sum=p1+p2;
prod=p1.*p2;

cpplib:sum_prod 中sum_prod为生成的文件名，sum_prod.m 为函数所在的文件名

库目录添加生成lib的所在目录和matlab的lib目录，我的是D:\Documents\MATLAB;D:\matlab\extern\lib\win64\microsoft;

sum_prod.lib;mclmcrrt.lib;mclmcr.lib

#include <iostream>
#include "sum_prod.h"
using namespace std;
int main()
{
if (!sum_prodInitialize())  //必须写
{
//cout << "初始化失败！" << endl;
return -1;
}
double p[2][2] = { 1, 2, 3, 4 };
mwArray X(2, 2, mxDOUBLE_CLASS);
mwArray Y(2, 2, mxDOUBLE_CLASS);
mwArray SUM(2, 2, mxDOUBLE_CLASS);
mwArray PROD(2, 2, mxDOUBLE_CLASS);
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 2; j++)
{
X(i + 1, j + 1) = p[i][j];
Y(i + 1, j + 1) = p[i][j];
}
}
sum_prod(2, SUM, PROD, X, Y);

double  sum[2][2], prod[2][2];
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 2; j++)
{
sum[i][j]=SUM(i + 1, j + 1) ;
prod[i][j]=PROD(i + 1, j + 1) ;
}
}
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 2; j++)
{
cout << sum[i][j];
}
cout << endl;
}
cout << endl;
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 2; j++)
{
cout << prod[i][j];
}
cout << endl;
}
cout << endl;
system("pause");
return 0;
}

01-24 8023
01-06
09-22 2万+
09-05 1万+
02-14 691
08-25 3962
07-15 3万+
03-19 455
11-18 1756
03-14 1万+
02-18 9392
09-08 711
01-04 546
03-20 5341
11-18 4317