MATLAB: error using drawnow: Interrupt while evaluating uicontrol Callback
조회 수: 9 (최근 30일)
이전 댓글 표시
Hi, I have a problem with a while loop inside a switch-case statement, used together with callback functions and "drawnow". In my code, while the cases of the switch-case are determined by pushbuttons in uicontrol, the case statements involves further callback functions to track mouse movements using 'windowbuttondown/up/motionfcn's. Because I draw multiple plots inside the while loop in the case statement, however, I use 'drawnow', which gives me the following error when I run the programme:
Error on line 160 ==> drawnow ??? Interrupt while evaluating uicontrol Callback
The piece of code inside the case statement gives no error when I run independently but somehow creates problem when is integrated with the rest of the code, which I attach below. Any help would be so much appreciated. Many thanks!
function programme(selection)
if nargin == 0
selection=0
end
switch selection
case 0 %start GUI and uicontrols to set up the cases i.e %programme(1), programme(2) etc
uicontrol('style','pushbutton',...
'string','First', ...
'position',[50 700 50 20], ...
'callback','programme(1);');
uicontrol('style','pushbutton',...
'string','Second', ...
'position',[150 700 50 20], ...
'callback','programme(2);');
case 1
%mouse track:
set(gcf,'windowbuttondownfcn','mousedown=1;');
set(gcf,'windowbuttonupfcn','mouseup=1;');
set(gcf,'windowbuttonmotionfcn','mousemotion=1;');
%to terminate the while loop, set up stopit=1 on one of uicontrol buttons:
uicontrol('style','pushbutton',...
'string','First', ...
'position',[50 700 50 20], ...
'callback','stopit=1;');
stopit=0;
while (stopit==0)
if mousedown==1
statements
if mouseup ==1
statements (plots)
mouseup=0;
mousedown=0;
mousedown=0;
end
end
drawnow
end
case 2
statements
otherwise
statements
end
댓글 수: 0
채택된 답변
Sean de Wolski
2012년 5월 9일
The moral of the story is: Don't use a while loop for this!
When the mouse is pushed the windowbuttondownfcn callback is fired. Inside this, change the windowbuttonmotionfcn to be your set of statements to execute while the button is down. Then when the windowbuttonupfcn is fired (i.e. release the mouse) reset the windowbuttonmotionfcn and fire the statements that you want for when they let up.
The solutions here may interest you:
댓글 수: 0
추가 답변 (2개)
Jan
2012년 5월 9일
Because I cannot reproduce the error, I cannot guess its reason. Could you please post a program, which is working?
But I see another problem: What do you expect the callback "stopit=1;" to do? It modifies the variable "stopit" in the base-workspace, such that you can check it in the command window. But this does not matter the variable "stopit" inside your function "programme". I suggest to read Matt's GUI examples to learn how to use callbacks: FEX: 41 GUI examples.
참고 항목
카테고리
Help Center 및 File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!