Skip to content

Commit c054a21

Browse files
author
Hans-Peter Wieser
authored
Merge pull request #400 from wahln/master_hotFixOptimizationCallback
Hotfix for Matlab batch mode crashing in optimization
2 parents 21cabfe + f1d46d6 commit c054a21

File tree

1 file changed

+15
-9
lines changed

1 file changed

+15
-9
lines changed

optimization/optimizer/matRad_OptimizerIPOPT.m

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -122,18 +122,24 @@
122122
fprintf('Press q to terminate the optimization...\n');
123123

124124
% set Callback
125-
125+
qCallbackSet = false;
126126
if ~isdeployed % only if _not_ running as standalone
127127
switch obj.env
128128
case 'MATLAB'
129-
% get handle to Matlab command window
130-
mde = com.mathworks.mde.desk.MLDesktop.getInstance;
131-
cw = mde.getClient('Command Window');
132-
xCmdWndView = cw.getComponent(0).getViewport.getComponent(0);
133-
h_cw = handle(xCmdWndView,'CallbackProperties');
129+
try
130+
% get handle to Matlab command window
131+
mde = com.mathworks.mde.desk.MLDesktop.getInstance;
132+
cw = mde.getClient('Command Window');
133+
xCmdWndView = cw.getComponent(0).getViewport.getComponent(0);
134+
h_cw = handle(xCmdWndView,'CallbackProperties');
134135

135-
% set Key Pressed Callback of Matlab command window
136-
set(h_cw, 'KeyPressedCallback', @(h,event) obj.abortCallbackKey(h,event));
136+
% set Key Pressed Callback of Matlab command window
137+
set(h_cw, 'KeyPressedCallback', @(h,event) obj.abortCallbackKey(h,event));
138+
fprintf('Press q to terminate the optimization...\n');
139+
qCallbackSet = true;
140+
catch
141+
fprintf('Manual optimization termination with q disabled.\n');
142+
end
137143
end
138144
end
139145

@@ -144,7 +150,7 @@
144150
[obj.wResult, obj.resultInfo] = ipopt(w0,funcs,ipoptStruct);
145151

146152
% unset Key Pressed Callback of Matlab command window
147-
if ~isdeployed && strcmp(obj.env,'MATLAB')
153+
if qCallbackSet
148154
set(h_cw, 'KeyPressedCallback',' ');
149155
end
150156

0 commit comments

Comments
 (0)