4

I am running the drawnow statement from a callback function within a MATLAB GUI to update the state of a button. At the beginning of the callback (which has high runtime) I alter the properties of the button and force an update with drawnow. While updateing properly, the button remains rendered 'pushed down' instead of 'disabled'. After the callback is finished, the button is updated again and now rendered 'disabled'.

Take following minmal (not) working example:

function simple_example()
h = figure();
% add a button, give it some properties and a callback
uicontrol(h,...
    'Style','pushbutton',...
    'String','I am enabled',...
    'Units','normalized',...
    'Position',[0.5 0.5 0.4 0.4],...
    'Callback',@btn_callback);
end

function btn_callback(hObject, ~)
    set(hObject,'Enable','off');
    set(hObject,'String','I am disabled');
    drawnow;
    pause(3);
end

Is there a way to change this behavior and have the button appear disabled while the callback is still executing?

1 Answers1

1

As you are asking about appearance here's a workaround using uibuttongroup:

function simple_example()
h = figure();
b = uibuttongroup('Position',[0.5 0.5 0.4 0.4]);
bgcolor = b.BackgroundColor;
% add a button, give it some properties and a callback
uicontrol(b,...
    'Style','pushbutton',...
    'String','I am enabled',...
    'Units','normalized',...
    'Position',[-0.05 -0.05 1.1 1.1],...
    'Callback',@btn_callback);
end

function btn_callback(hObject, ~)
    set(hObject,'Enable','off');
    set(hObject,'String','I am disabled');
    drawnow;
    pause(3);
end

Here, you fit the button within a uibuttongroup, which normally groups several uibuttons and then set the button size bigger than the actual uibuttongroup, so the borders don't appear.

However, this let's you lose the button down rendering. You could get that back by altering the uicontrolgroup's border properties.

Update:

This seems to be OS-specific. On OS X your code works just fine, as far as I can see. Windows, I don't know, but according to your comment neither my version, nor yours seems to fix the issue. On Ubuntu, on the other hand, my answer solves the problem.

lhcgeneva
  • 1,981
  • 2
  • 21
  • 29
  • Sadly, this doesn't solve my problem. I am running R2015a on a win10 OS. When I try your example, the thick blue border around the button doesn't get displayed. However the entire button seems to get a blue overlay when you click on it (which seems to come from callback not from it beeng the currently selected object). If that could be removed as well, I can set color and background color to look the same as a disabled button. –  Nov 21 '15 at 14:12
  • It might be OS specific. I just tried on a wn10 and a win8 machine (both using R2015a) and both show the same issue with either version of above example. –  Nov 21 '15 at 14:26