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!