MATLAB: Set class method as CloseRequestFcn

classcloserequestfcnmethodsoop

I am currently working on a waitbar that is implemented as a class. I need to detect when the user clicks the X-button of the window to cancel computations and then set a flag.
Considering the following class:
classdef myWaitbar < handle
properties
figHandle
cancel
end
methods
function obj = myWaitbar()
obj.cancel = false;
obj.figHandle = figure('CloseRequestFcn', @...);
end
function setFlag(obj)
obj.cancel = true;
end
end
end
Does anybody know how to declare CloseRequestFcn and setFlag to make this work? I tried a few different approaches but could not find a proper way.
Thank you

Best Answer

Sebastian - you can try the following
function obj = myWaitbar()
obj.cancel = false;
obj.figHandle = figure('CloseRequestFcn', @(h,e)obj.setFlag);
end
function setFlag(hObject,eventdata)
hObject.cancel = true;
delete(hObject.figHandle);
end
The setFlag method will be called when the x is pressed in the corner of the wait bar figure. (At least it does for me when using R2014a.) I'm not sure how you will report the change to cancel though. Do you have "something" listening or waiting for it to change value?