cancelling waitbar insdie a gui function
1 次查看(过去 30 天)
显示 更早的评论
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.
0 个评论
采纳的回答
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 个评论
Jan
2017-4-20
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 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Dialog Boxes 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!