Code covered by the BSD License  

Highlights from
Increment/Decrement uicontrol

image thumbnail
from Increment/Decrement uicontrol by Chad Gilbert
A custom uicontrol for making increment/decrement button pairs.

incrementdecrement(lblhdl, initval, varargin)
function [grp] = incrementdecrement(lblhdl, initval, varargin)
%INCREMENTDECREMENT create a button group for incrementing/decrementing a value.
%   INCREMENTDECREMENT is a custom button group that simplifies the creation of
%   the "inc/dec" arrows that are commonly used in GUIs.
%
%   INCREMENTDECREMENT(INITVAL) creates the button group and sets the initial

% Create the button group, which will contain the inc/dec buttons.
grp = uibuttongroup(varargin{:});

% Get sizing info for the buttons.
set(grp, 'Units', 'Points')
pos = get(grp, 'Position');
height = pos(4)/2;
posup = [-1 height-1 pos(3) height];
posdn = [-1 -1       pos(3) height];

% Create the buttons.
hup = uicontrol('Style', 'PushButton', 'Parent', grp);
hdn = uicontrol('Style', 'PushButton', 'Parent', grp);

% Resize the buttons to fill the group.
set(hup, 'Units', 'Points', 'Position', posup);
set(hdn, 'Units', 'Points', 'Position', posdn);

% From http://www.mathworks.com/matlabcentral/newsreader/view_thread/51230.
set(hup, 'String', '<html>&#x25B2;</html>', 'FontSize', height/1.5);
set(hdn, 'String', '<html>&#x25BC;</html>', 'FontSize', height/1.5);

% Initalize the counter.
set(grp, 'UserData', initval);

% Set the callbacks.
set(hup, 'Callback', @inc);
set(hdn, 'Callback', @dec);

% Define the callbacks.
    function inc(varargin)
        set(grp, 'UserData', get(grp, 'UserData') + 1);
        set(lblhdl, 'Value', get(grp, 'UserData'));
        set(lblhdl, 'String', num2str(get(grp, 'UserData')));
    end

    function dec(varargin)
        set(grp, 'UserData', get(grp, 'UserData') - 1);
        set(lblhdl, 'Value', get(grp, 'UserData'));
        set(lblhdl, 'String', num2str(get(grp, 'UserData')));
    end

end

Contact us