Matlab不但提供了与C/C++混合编程,也提供了!和system调用C/C++生成的exe文件。因为这个在实际不常使用,Matlab的help文档中的实例感觉不是很好,我初次使用时尝试了好几次才成功。今天就在此说明下。
在此只说system调用exe文件的使用过程,!调用exe没用过,但应该差不多,Matlab中的help文档有说明。调用exe文件主要有下面几个步骤:
1、编写C/C++源文件,在此我编写了向一个函数传入两个数字,求其和,主函数名为:Win32CosoleW。
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;
int main(int argc, char* argv[])
{
double Operand1, Operand2, Addition;
Operand1 = atoi(argv[1]);//字符串转化为数字
Operand2 = atoi(argv[2]);
Addition = Operand1 + Operand2;
cout << "\nFirst Number: " << Operand1;
cout << "\nSecond Number: " << Operand2 << endl;
cout << Operand1 << " + " << Operand2 << " = " << Addition << endl;
return 0;
}
2、单击VS菜单中的 生成->生成解决方案 后(此时不要),拷贝Win32CosoleW.exe,到Matlab任意文件夹下。
3、编写Matlab 脚本或函数文件,我在写个小脚本。
clc; ExeFileName='Win32CosoleW.exe'; ExeFilePath=fullfile('.\',ExeFileName); Param1=[' ','1'];%第一个参数,一定要有' ' Param2=[' ','15']; Cmd=[ExeFilePath ,Param1 ,Param2]; system(Cmd);至此Matlab调用exe的过程就这样了,运行一下Matlab脚本将会得到如下结果:
First Number: 1
Second Number: 15
1 + 15 = 16