介绍
TCP如此常用的通讯功能,matlab自然也是支持的。而在用途方面也有很多,比如matlab优化问题中目标函数可以是其他程序的运算结果,此时需要动态的每次优化调用其他程序,这时可以使用tcp实现两程序之间的数据交互,由matlab的优化工具箱提供每次优化的设计变量值,有某程序提供基于设计变量得出的运算结果值作为目标函数的解。
TCP使用方法
Matlab 对TCP的封装很好,只有在建立和关闭tcp需要专用的函数,其他操作均与文件操作完全一致。
下面先给出一个用于优化的范例:
主脚本:
clc;clear;
%开启tcp
global mtcpip %用全局变量方便优化函数中使用tcp
global circle_number
circle_number=0;
mtcpip=tcpip('127.0.0.1', 8000);
fopen(mtcpip);
fwrite(mtcpip,['Matlab Connect in',char(13), char(10)])
%优化算法--
current_value = [123123 123123 123123 123321 123123 123123 13 123123 123123];
lb=[];
ub=[];
new_error = fmincon(@matlab_function,current_value,[],[],[],[],lb,ub);%优化
% 优化技术-关闭tcp
fwrite(mtcpip,['Matlab Disconnect',char(13), char(10)])
fclose(mtcpip);
delete(mtcpip);