我有一个 .mat 文件中的实验数据和一个用 dymola 编写的模型。我使用 dymolaM.m 文件将命令从 matlab 发送到 dymola。这样我就可以使用 matlab 来优化我的模型的参数。Matlab 从 .mat 文件中读取数据,以正确的方式初始化 dymola,让 dymola 模拟模型,读取 dymola 的输出并将其与实验数据进行比较。
这一切都很好,但如果我重复 matlab dymola 连接太多,我总是会收到以下错误:
??? Error using ==> ddeexec
The first argument 'channel' is not valid.
Error in ==> dymolaM at 59
status = ddeexec(DymolaChannel, deblank(command(i,:)), '', timeout);
我尝试重复完全相同的命令 200 次。我总是在第 26 次迭代时遇到错误。
我应该怎么办?这是 dymolaM.m 文件的代码
function res=dymolaM(command, value)
% Executes a command optionally followed by a value in Dymola.
% Dymola is started if not already running.
% Version 1.0, 1997-11-14
% Version 1.1, 1999-06-12
% Copyright (C) 1997-2001 Dynasim AB.
% All rights reserved.
DymolaChannel = ddeinit('dymola', ' ');
if DymolaChannel == 0,
pause(1);
DymolaChannel = ddeinit('dymola', ' ');
if DymolaChannel == 0,
dymola = getenv('DYMOLA');
v=version;
if (isempty(dymola) & v(1,1)>='5')
if v(1,1)>='7'
[st,I]=dbstack('-completenames');
else
[st,I]=dbstack;
end
if v(1,1)>='7'
currentfile=st(I).file;
else
currentfile=st(I).name;
end
I=find(currentfile=='\' | currentfile=='/');
currentfile=currentfile(1:(max([1,I(1:end-1)])-1));
dymola=currentfile;
end
if ~isempty(dymola)
dymola=[dymola,'\bin\'];
end
disp('Starting Dymola - Wait');
fquote = '';
if strcmp(computer, 'PCWIN') | strcmp(computer, 'PCWIN64')
fquote = '"';
end;
dos([fquote,dymola,'Dymola',fquote,' &']);
pause(10);
DymolaChannel = ddeinit('dymola', ' ');
disp('Dymola started');
else
disp('Worked around problem communicating with Dymola');
end
end
if nargout>0
res=zeros(size(command,1),1);
end
if DymolaChannel == 0,
disp('Error starting Dymola');
elseif nargin > 0,
for i=1:size(command,1)
timeout=4000*1000; % Can always kill dymola
if nargin == 1,
status = ddeexec(DymolaChannel, deblank(command(i,:)), '', timeout);
elseif nargin == 2,
status = ddeexec(DymolaChannel, [deblank(command(i,:)), num2str(value(i))], '', timeout);
end
if nargout==0
if status == 0,
error('Invalid Dymola command.');
end
else
res(i)=status;
end;
end
ddeterm(DymolaChannel);
end