Set class method as CloseRequestFcn
7 views (last 30 days)
Show older comments
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
0 Comments
Accepted Answer
Geoff Hayes
on 3 Feb 2017
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?
2 Comments
Guillaume
on 4 Feb 2017
Edited: Guillaume
on 4 Feb 2017
Hum, I believe the anonymous function should be:
@(h,e) obj.setFlag(e)
%or
@(~, e) obj.setFlag(e)
As it is you'll get a not enough input arguments error in setFlag.
And I find calling hObject the first argument of setFlag misleading as it seems to implies it's the h of the @(h,e) whereas it's actually the obj of obj.setFlag, so I'd have:
function setFlag(obj, eventdata)
obj.cancel = true;
delete(obj.fighandle);
end
Or to make everything even clearer:
function obj = myWaitbar()
obj.cancel = false;
obj.figHandle = figure('CloseRequestFcn', @(h,e)obj.setFlag(h, e));
end
function setFlag(obj, hsource, eventdata) %eventdata could be replaced by ~
obj.cancel = true;
delete(hsource);
end
Third option is:
function obj = myWaitbar()
obj.cancel = false;
obj.figHandle = figure('CloseRequestFcn', @(~,~)obj.setFlag);
end
function setFlag(obj)
obj.cancel = true;
delete(obj.figHandle);
end
More Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!