MATLAB: error using drawnow: Interrupt while evaluating uicontrol Callback

7 次查看(过去 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

采纳的回答

Sean de Wolski
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:

更多回答(2 个)

Jan
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.
  1 个评论
inci
inci 2012-5-9
The problem seems to be with the while loop used together with drawnow and callback functions but I cannot think of another way of doing what I am trying to do.

请先登录,再进行评论。


inci
inci 2012-5-10
Hello,
Thank you very much both for the answers! Following Sean's comment, I changed my code with the help of the solutions on the graphics challenge link and it seems to be working now.

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by