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>▲</html>', 'FontSize', height/1.5);
set(hdn, 'String', '<html>▼</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