Find Selected button in Button Group CallBack
I have a group of 5 buttons (including a dummy one for "none"
In the callback I want to do something based on the selected button.
Here is the default start of the callback:
function ManualRewardsButtonGroupSelectionChanged(app, event)
selectedButton = app.ManualRewardsButtonGroup.SelectedObject;
So why is the selectedButton always 1? I’d like 1 to 5 so I can do the appropriate
switch app.ManualRewardsButtonGroup.NewValue % or what here?
case 1
% Give LA reward
case 2
% Give LB reward
case 3
% Give RA reward
case 4
% Give RA reward
end
Help shows that I could use event.NewValue (since selectedButton doesn’t work) but that also returns 1
https://www.mathworks.com/help/matlab/ref/matlab.ui.container.buttongroup-properties.html
Van I set each button to have a different value? (I clicked on the Value check box in the Component but no opportunity to set it. I’ve even tried assigning Tags though I don’t know what they do.
App Designer doesn’t set values and doesn’t let me do it either
% Create ManualRewardsButtonGroup
app.ManualRewardsButtonGroup = uibuttongroup(app.RewardControls);
app.ManualRewardsButtonGroup.AutoResizeChildren = ‘off’;
app.ManualRewardsButtonGroup.SelectionChangedFcn = createCallbackFcn(app, @ManualRewardsButtonGroupSelectionChanged, true);
app.ManualRewardsButtonGroup.TitlePosition = ‘centertop’;
app.ManualRewardsButtonGroup.Title = ‘Manual Rewards’;
app.ManualRewardsButtonGroup.FontSize = 10;
app.ManualRewardsButtonGroup.Position = [38 6 190 95];
% Create Rew_ALeftButton
app.Rew_ALeftButton = uiradiobutton(app.ManualRewardsButtonGroup);
app.Rew_ALeftButton.Tag = ‘1’;
app.Rew_ALeftButton.Text = ‘Rew_A Left’;
app.Rew_ALeftButton.FontSize = 10;
app.Rew_ALeftButton.Position = [11 49 73 22];
% Create Rew_ARightButton
app.Rew_ARightButton = uiradiobutton(app.ManualRewardsButtonGroup);
app.Rew_ARightButton.Tag = ‘2’;
app.Rew_ARightButton.Text = ‘Rew_A Right’;
app.Rew_ARightButton.FontSize = 10;
app.Rew_ARightButton.Position = [101 49 80 22];
% Create Rew_BLeftButton
app.Rew_BLeftButton = uiradiobutton(app.ManualRewardsButtonGroup);
app.Rew_BLeftButton.Tag = ‘3’;
app.Rew_BLeftButton.Text = ‘Rew_B Left’;
app.Rew_BLeftButton.FontSize = 10;
app.Rew_BLeftButton.Position = [11 27 74 22];
% Create Rew_BRightButton
app.Rew_BRightButton = uiradiobutton(app.ManualRewardsButtonGroup);
app.Rew_BRightButton.Tag = ‘4’;
app.Rew_BRightButton.Text = ‘Rew_B Right’;
app.Rew_BRightButton.FontSize = 10;
app.Rew_BRightButton.Position = [101 27 80 22];
app.Rew_BRightButton.Value = true;
% Create OffButton
app.OffButton = uiradiobutton(app.ManualRewardsButtonGroup);
app.OffButton.Text = ‘Off’;
app.OffButton.FontSize = 10;
app.OffButton.Position = [85 5 35 22];% Create ManualRewardsButtonGroup
app.ManualRewardsButtonGroup = uibuttongroup(app.RewardControls);
app.ManualRewardsButtonGroup.AutoResizeChildren = ‘off’;
app.ManualRewardsButtonGroup.SelectionChangedFcn = createCallbackFcn(app, @ManualRewardsButtonGroupSelectionChanged, true);
app.ManualRewardsButtonGroup.TitlePosition = ‘centertop’;
app.ManualRewardsButtonGroup.Title = ‘Manual Rewards’;
app.ManualRewardsButtonGroup.FontSize = 10;
app.ManualRewardsButtonGroup.Position = [38 6 190 95];
% Create Rew_ALeftButton
app.Rew_ALeftButton = uiradiobutton(app.ManualRewardsButtonGroup);
app.Rew_ALeftButton.Tag = ‘1’;
app.Rew_ALeftButton.Text = ‘Rew_A Left’;
app.Rew_ALeftButton.FontSize = 10;
app.Rew_ALeftButton.Position = [11 49 73 22];
% Create Rew_ARightButton
app.Rew_ARightButton = uiradiobutton(app.ManualRewardsButtonGroup);
app.Rew_ARightButton.Tag = ‘2’;
app.Rew_ARightButton.Text = ‘Rew_A Right’;
app.Rew_ARightButton.FontSize = 10;
app.Rew_ARightButton.Position = [101 49 80 22];
% Create Rew_BLeftButton
app.Rew_BLeftButton = uiradiobutton(app.ManualRewardsButtonGroup);
app.Rew_BLeftButton.Tag = ‘3’;
app.Rew_BLeftButton.Text = ‘Rew_B Left’;
app.Rew_BLeftButton.FontSize = 10;
app.Rew_BLeftButton.Position = [11 27 74 22];
% Create Rew_BRightButton
app.Rew_BRightButton = uiradiobutton(app.ManualRewardsButtonGroup);
app.Rew_BRightButton.Tag = ‘4’;
app.Rew_BRightButton.Text = ‘Rew_B Right’;
app.Rew_BRightButton.FontSize = 10;
app.Rew_BRightButton.Position = [101 27 80 22];
app.Rew_BRightButton.Value = true;
% Create OffButton
app.OffButton = uiradiobutton(app.ManualRewardsButtonGroup);
app.OffButton.Text = ‘Off’;
app.OffButton.FontSize = 10;
app.OffButton.Position = [85 5 35 22];I have a group of 5 buttons (including a dummy one for "none"
In the callback I want to do something based on the selected button.
Here is the default start of the callback:
function ManualRewardsButtonGroupSelectionChanged(app, event)
selectedButton = app.ManualRewardsButtonGroup.SelectedObject;
So why is the selectedButton always 1? I’d like 1 to 5 so I can do the appropriate
switch app.ManualRewardsButtonGroup.NewValue % or what here?
case 1
% Give LA reward
case 2
% Give LB reward
case 3
% Give RA reward
case 4
% Give RA reward
end
Help shows that I could use event.NewValue (since selectedButton doesn’t work) but that also returns 1
https://www.mathworks.com/help/matlab/ref/matlab.ui.container.buttongroup-properties.html
Van I set each button to have a different value? (I clicked on the Value check box in the Component but no opportunity to set it. I’ve even tried assigning Tags though I don’t know what they do.
App Designer doesn’t set values and doesn’t let me do it either
% Create ManualRewardsButtonGroup
app.ManualRewardsButtonGroup = uibuttongroup(app.RewardControls);
app.ManualRewardsButtonGroup.AutoResizeChildren = ‘off’;
app.ManualRewardsButtonGroup.SelectionChangedFcn = createCallbackFcn(app, @ManualRewardsButtonGroupSelectionChanged, true);
app.ManualRewardsButtonGroup.TitlePosition = ‘centertop’;
app.ManualRewardsButtonGroup.Title = ‘Manual Rewards’;
app.ManualRewardsButtonGroup.FontSize = 10;
app.ManualRewardsButtonGroup.Position = [38 6 190 95];
% Create Rew_ALeftButton
app.Rew_ALeftButton = uiradiobutton(app.ManualRewardsButtonGroup);
app.Rew_ALeftButton.Tag = ‘1’;
app.Rew_ALeftButton.Text = ‘Rew_A Left’;
app.Rew_ALeftButton.FontSize = 10;
app.Rew_ALeftButton.Position = [11 49 73 22];
% Create Rew_ARightButton
app.Rew_ARightButton = uiradiobutton(app.ManualRewardsButtonGroup);
app.Rew_ARightButton.Tag = ‘2’;
app.Rew_ARightButton.Text = ‘Rew_A Right’;
app.Rew_ARightButton.FontSize = 10;
app.Rew_ARightButton.Position = [101 49 80 22];
% Create Rew_BLeftButton
app.Rew_BLeftButton = uiradiobutton(app.ManualRewardsButtonGroup);
app.Rew_BLeftButton.Tag = ‘3’;
app.Rew_BLeftButton.Text = ‘Rew_B Left’;
app.Rew_BLeftButton.FontSize = 10;
app.Rew_BLeftButton.Position = [11 27 74 22];
% Create Rew_BRightButton
app.Rew_BRightButton = uiradiobutton(app.ManualRewardsButtonGroup);
app.Rew_BRightButton.Tag = ‘4’;
app.Rew_BRightButton.Text = ‘Rew_B Right’;
app.Rew_BRightButton.FontSize = 10;
app.Rew_BRightButton.Position = [101 27 80 22];
app.Rew_BRightButton.Value = true;
% Create OffButton
app.OffButton = uiradiobutton(app.ManualRewardsButtonGroup);
app.OffButton.Text = ‘Off’;
app.OffButton.FontSize = 10;
app.OffButton.Position = [85 5 35 22];% Create ManualRewardsButtonGroup
app.ManualRewardsButtonGroup = uibuttongroup(app.RewardControls);
app.ManualRewardsButtonGroup.AutoResizeChildren = ‘off’;
app.ManualRewardsButtonGroup.SelectionChangedFcn = createCallbackFcn(app, @ManualRewardsButtonGroupSelectionChanged, true);
app.ManualRewardsButtonGroup.TitlePosition = ‘centertop’;
app.ManualRewardsButtonGroup.Title = ‘Manual Rewards’;
app.ManualRewardsButtonGroup.FontSize = 10;
app.ManualRewardsButtonGroup.Position = [38 6 190 95];
% Create Rew_ALeftButton
app.Rew_ALeftButton = uiradiobutton(app.ManualRewardsButtonGroup);
app.Rew_ALeftButton.Tag = ‘1’;
app.Rew_ALeftButton.Text = ‘Rew_A Left’;
app.Rew_ALeftButton.FontSize = 10;
app.Rew_ALeftButton.Position = [11 49 73 22];
% Create Rew_ARightButton
app.Rew_ARightButton = uiradiobutton(app.ManualRewardsButtonGroup);
app.Rew_ARightButton.Tag = ‘2’;
app.Rew_ARightButton.Text = ‘Rew_A Right’;
app.Rew_ARightButton.FontSize = 10;
app.Rew_ARightButton.Position = [101 49 80 22];
% Create Rew_BLeftButton
app.Rew_BLeftButton = uiradiobutton(app.ManualRewardsButtonGroup);
app.Rew_BLeftButton.Tag = ‘3’;
app.Rew_BLeftButton.Text = ‘Rew_B Left’;
app.Rew_BLeftButton.FontSize = 10;
app.Rew_BLeftButton.Position = [11 27 74 22];
% Create Rew_BRightButton
app.Rew_BRightButton = uiradiobutton(app.ManualRewardsButtonGroup);
app.Rew_BRightButton.Tag = ‘4’;
app.Rew_BRightButton.Text = ‘Rew_B Right’;
app.Rew_BRightButton.FontSize = 10;
app.Rew_BRightButton.Position = [101 27 80 22];
app.Rew_BRightButton.Value = true;
% Create OffButton
app.OffButton = uiradiobutton(app.ManualRewardsButtonGroup);
app.OffButton.Text = ‘Off’;
app.OffButton.FontSize = 10;
app.OffButton.Position = [85 5 35 22]; I have a group of 5 buttons (including a dummy one for "none"
In the callback I want to do something based on the selected button.
Here is the default start of the callback:
function ManualRewardsButtonGroupSelectionChanged(app, event)
selectedButton = app.ManualRewardsButtonGroup.SelectedObject;
So why is the selectedButton always 1? I’d like 1 to 5 so I can do the appropriate
switch app.ManualRewardsButtonGroup.NewValue % or what here?
case 1
% Give LA reward
case 2
% Give LB reward
case 3
% Give RA reward
case 4
% Give RA reward
end
Help shows that I could use event.NewValue (since selectedButton doesn’t work) but that also returns 1
https://www.mathworks.com/help/matlab/ref/matlab.ui.container.buttongroup-properties.html
Van I set each button to have a different value? (I clicked on the Value check box in the Component but no opportunity to set it. I’ve even tried assigning Tags though I don’t know what they do.
App Designer doesn’t set values and doesn’t let me do it either
% Create ManualRewardsButtonGroup
app.ManualRewardsButtonGroup = uibuttongroup(app.RewardControls);
app.ManualRewardsButtonGroup.AutoResizeChildren = ‘off’;
app.ManualRewardsButtonGroup.SelectionChangedFcn = createCallbackFcn(app, @ManualRewardsButtonGroupSelectionChanged, true);
app.ManualRewardsButtonGroup.TitlePosition = ‘centertop’;
app.ManualRewardsButtonGroup.Title = ‘Manual Rewards’;
app.ManualRewardsButtonGroup.FontSize = 10;
app.ManualRewardsButtonGroup.Position = [38 6 190 95];
% Create Rew_ALeftButton
app.Rew_ALeftButton = uiradiobutton(app.ManualRewardsButtonGroup);
app.Rew_ALeftButton.Tag = ‘1’;
app.Rew_ALeftButton.Text = ‘Rew_A Left’;
app.Rew_ALeftButton.FontSize = 10;
app.Rew_ALeftButton.Position = [11 49 73 22];
% Create Rew_ARightButton
app.Rew_ARightButton = uiradiobutton(app.ManualRewardsButtonGroup);
app.Rew_ARightButton.Tag = ‘2’;
app.Rew_ARightButton.Text = ‘Rew_A Right’;
app.Rew_ARightButton.FontSize = 10;
app.Rew_ARightButton.Position = [101 49 80 22];
% Create Rew_BLeftButton
app.Rew_BLeftButton = uiradiobutton(app.ManualRewardsButtonGroup);
app.Rew_BLeftButton.Tag = ‘3’;
app.Rew_BLeftButton.Text = ‘Rew_B Left’;
app.Rew_BLeftButton.FontSize = 10;
app.Rew_BLeftButton.Position = [11 27 74 22];
% Create Rew_BRightButton
app.Rew_BRightButton = uiradiobutton(app.ManualRewardsButtonGroup);
app.Rew_BRightButton.Tag = ‘4’;
app.Rew_BRightButton.Text = ‘Rew_B Right’;
app.Rew_BRightButton.FontSize = 10;
app.Rew_BRightButton.Position = [101 27 80 22];
app.Rew_BRightButton.Value = true;
% Create OffButton
app.OffButton = uiradiobutton(app.ManualRewardsButtonGroup);
app.OffButton.Text = ‘Off’;
app.OffButton.FontSize = 10;
app.OffButton.Position = [85 5 35 22];% Create ManualRewardsButtonGroup
app.ManualRewardsButtonGroup = uibuttongroup(app.RewardControls);
app.ManualRewardsButtonGroup.AutoResizeChildren = ‘off’;
app.ManualRewardsButtonGroup.SelectionChangedFcn = createCallbackFcn(app, @ManualRewardsButtonGroupSelectionChanged, true);
app.ManualRewardsButtonGroup.TitlePosition = ‘centertop’;
app.ManualRewardsButtonGroup.Title = ‘Manual Rewards’;
app.ManualRewardsButtonGroup.FontSize = 10;
app.ManualRewardsButtonGroup.Position = [38 6 190 95];
% Create Rew_ALeftButton
app.Rew_ALeftButton = uiradiobutton(app.ManualRewardsButtonGroup);
app.Rew_ALeftButton.Tag = ‘1’;
app.Rew_ALeftButton.Text = ‘Rew_A Left’;
app.Rew_ALeftButton.FontSize = 10;
app.Rew_ALeftButton.Position = [11 49 73 22];
% Create Rew_ARightButton
app.Rew_ARightButton = uiradiobutton(app.ManualRewardsButtonGroup);
app.Rew_ARightButton.Tag = ‘2’;
app.Rew_ARightButton.Text = ‘Rew_A Right’;
app.Rew_ARightButton.FontSize = 10;
app.Rew_ARightButton.Position = [101 49 80 22];
% Create Rew_BLeftButton
app.Rew_BLeftButton = uiradiobutton(app.ManualRewardsButtonGroup);
app.Rew_BLeftButton.Tag = ‘3’;
app.Rew_BLeftButton.Text = ‘Rew_B Left’;
app.Rew_BLeftButton.FontSize = 10;
app.Rew_BLeftButton.Position = [11 27 74 22];
% Create Rew_BRightButton
app.Rew_BRightButton = uiradiobutton(app.ManualRewardsButtonGroup);
app.Rew_BRightButton.Tag = ‘4’;
app.Rew_BRightButton.Text = ‘Rew_B Right’;
app.Rew_BRightButton.FontSize = 10;
app.Rew_BRightButton.Position = [101 27 80 22];
app.Rew_BRightButton.Value = true;
% Create OffButton
app.OffButton = uiradiobutton(app.ManualRewardsButtonGroup);
app.OffButton.Text = ‘Off’;
app.OffButton.FontSize = 10;
app.OffButton.Position = [85 5 35 22]; buttongroup, selectedbutton MATLAB Answers — New Questions