user2184466
user2184466

Reputation: 21

How do I alter a variable with a uicontrol callback function?

The big picture goal is to successively plot an image sequence at some rate and pause the process when the pushbutton is pressed. Then resume the process upon hitting the space bar or pressing the pushbutton or whatever. Here is the code I have so far with a simple rand/pause(0.5) procedure for each iteration of the loop (instead of plotting my images).

My issue is this: ButtonCallback seems to be invoked when the pushbutton is pressed, and DoPause is set to 1 in the callback function, BUT DoPause is not changed in the main function PauseButtonTest. How do I change this variable so the if statement in my while loop is executed.

Any help is appreciated. Thanks.

function PauseButtonTest

  DoPause = 0;

  hb = uicontrol('Style','Pushbutton','String','Pause','FontWeight','Bold', ...
    'Callback',@ButtonCallback,'Unit','Normalized','Position',[0 0 1 1]);

  while ishandle(hb)

    if DoPause
      disp('You pushed the button')
      pause
      DoPause = 0;
      set(hb,'Enable','On')
      drawnow
    end

    % Computations/Plot successive figures/etc.
    rand
    pause(0.5)

  end

end


function ButtonCallback(hObj, event)

  DoPause = 1;
  disp('You are in ButtonCallback')
  disp(['DoPause = ' num2str(DoPause)])
  set(hObj,'Enable','off')

end

Upvotes: 1

Views: 348

Answers (1)

chappjc
chappjc

Reputation: 30579

Nested functions.

Just move function ButtonCallback(hObj, event) ... inside PauseButtonTest so that it has access to its variables ("externally scoped" variables). Plop it down a couple of lines below the while loop and before the end that signals the end of PauseButtonTest.

Upvotes: 1

Related Questions