matlabuser-interfacematlab-guidehandles

Need to press button twice to update variable in MATLAB GUI


I have the following code as part of my MATLAB GUI code:

k = waitforbuttonpress;
if k==0               
   if strcmp(get(handles.YESNO,'String'),'Y')

        hint = 1;
    else
        hint = 0;
    end                
else
    hint = 0;
end

I wait for the user to press one of the two YES or NO buttons. Inside each of these callbacks I update the variable handles.YESNO as set(handles.YESNO,'String','Y'); or set(handles.YESNO,'String','N'); respectively.

When I execute my MATLAB GUI, I have to press the YES button twice for the value to take into effect. Any tips/hints to overcome this issue?


Solution

  • I believe that the waitforbuttonpress mask your callback. Instead, block your executation with uiwait that waits for your figure to close or to uiresume called by the buttons callbacks.