I have data of an experiment in a .mat file and a model written in dymola. I use the dymolaM.m file to send commands from matlab to dymola. In that way I can use matlab to optimize a parameter of my model. Matlab reads in the data from the .mat file, initialises dymola in a correct way, lets dymola simulate the model, reads in the output of dymola and compares it with the experimental data.
This all works fine for a wile, but if I repeat the matlab dymola connection too much I always get the following error:
??? Error using ==> ddeexec
The first argument 'channel' is not valid.
Error in ==> dymolaM at 59
status = ddeexec(DymolaChannel, deblank(command(i,:)), '', timeout);
I tried repeating exactly the same command 200 times. I always got the error at iteration 26.
What should I do? This is the code of the dymolaM.m file
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