필터 지우기
필터 지우기

cancelling waitbar insdie a gui function

조회 수: 1 (최근 30일)
Shravan Kumar
Shravan Kumar 2017년 4월 19일
편집: Jan 2018년 6월 12일
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
Shravan Kumar
Shravan Kumar 2017년 4월 20일
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.
Jan
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 CenterFile Exchange에서 Dialog Boxes에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by