cancelling waitbar insdie a gui function

Hello everyone, I am having some problem with waitbar defined inside a gui script. The problem is I have created a waitbar with cancel option which was working fine outside. But when I pasted it inside my gui.m script the cancel button is not working (rest all is fine). Below is the code I have used:
pvh=waitbar(0,'calculating.....','name','ESTIMATION',...
'createcancelbtn','setappdata(pvh,''cancel_callback'',1)');
setappdata(pvh,'cancel_callback',0);
for count_i1 = 1:l_count
if getappdata(pvh,'cancel_callback')==1
errordlg('Stopped by user');
break;
end
waitbar(count_i1/ l_count,pvh,sprintf('Calculating %d %s %d',count_i1,'of', l_count))
I went through some help of MATLAB and tried some options like drawnow or updating the handles of the waitbar figure. Looks like I did not do it properly. Any suggestions on how to make it work? thanks in advance.

 采纳的回答

Jan
Jan 2017-4-19
编辑:Jan 2018-6-12
You cannot access the handle pvh from the callback, when it is defined as a string. Callbacks defines as a string are evaluated in the baseworkspace, where "phv" is not known. I'd expect an error message then.
Unfortunately the documentation of waitbar mentions a string-callback only. Using a function handle would be smarter. Try this:
pvh = waitbar(0, 'calculating.....', 'name', 'ESTIMATION',...
'createcancelbtn', @myCancelCB);
setappdata(pvh, 'cancel_callback', 0);
for count_i1 = 1:l_count
drawnow;
if getappdata(pvh, 'cancel_callback') == 1
errordlg('Stopped by user');
break; % Stop the loop: for coubnt_i1
end
...
end
function myCancelCB(hObject, EventData)
phv = ancestor(hObject, 'figure');
setappdata(phv, 'cancel_callback', 1);
end
This is somewhat longer than defining the callback as string, but much safer, because it does not pollute the baseworkspace.

2 个评论

Thank you for the solution Jan. I tried it and its just working fine. I have one small question, how did it work when defined in a script but not in gui.m. I defined this in the main call back function (inside gui) only right. so it did not need to update the handles right? yes, I also got the error as you said that undefined variable pvh. I went through the documentation of ancestor and got how this logic is working. Thanks again. For people who want to use the @myCancelCB function please remove the 'end' from the function. works fine.
The trailing "end" is required, if any function of the M-file has one or if it is a nested function. In other cases it should be omitted.
I do not understand exactly what "inside gui" means. The show code works in a script also, but in older Matlab versions you have to define the function for the callback in a separate file.
Where did you "update the handles"?
The general suggestions are:
  • Functions are more stable than scripts, so use scripts for short hacks only, while functions are preferred for productive work.
  • Define callbacks by function handles, because the evaluation of strings is prone to bugs and hard to debug.

请先登录,再进行评论。

更多回答(0 个)

类别

在 帮助中心 和 File Exchange 中查找有关 App Building 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by