Does anyone know of a cleaner solution for the following: I'm running a matlab script that might need to be killed at some point. Hitting "cntrl-C" works but pops open some random file in debug, and might still fail depending on if the figures are in the middle of drawing.
Best I could come up with: add a button to the figure I'm looking at, on mouse-click do "clear all". Simply doing "error" doesn't work because it throws an exception that some matlab function successfully catches and continues running.
Update / Clarification: the force-crash relies on clearing some global variable in the main script.
function myScript()
global foo;
foo = 1;
while 1
x = DoStuff();
sh = figure(1);
if k == 1
killable_window( sh );
end
x.display();
drawnow;
y = foo + 1; % <-- crashes if the callback does 'clear all', which kills global variable foo
end
end
Then this is the dirty version of a killable window:
function [] = killable_window( sh )
S.fh = sh;
S.pb = uicontrol('style','push',...
'units','pix',...
'position',[10 30 80 20],...
'fontsize',12,...
'string','Quit');
set(S.pb,'callback' ,{@pb_call,S})
% Check if 'p' is pressed when focus on button and exec callback
set(S.pb,'KeyPressFcn',{@pb_kpf ,S});
% Check if 'p' is pressed when focus on figure and exec callback
set(S.fh,'KeyPressFcn',{@pb_kpf ,S});
% Callback for pushbutton, clears all variables
function pb_call(varargin)
S = varargin{3}; % Get the structure.
fprintf('force quitting due to button press...\n');
% ghetto: clear everything to force a crash later
% and prevent anyone from successfully catching an exception
clear all;
end
% Do same action as button when pressed 'p'
function pb_kpf(varargin)
if varargin{1,2}.Character == 'p'
pb_call(varargin{:})
end
end
end
so, if I don't like what I see, I hit the "quit" button, and it dumps back to home screen, but I lose my variables in the process... is there a way to quit, or make "error" prevent anyone from catching the exceptions ?