Clear Filters
Clear Filters

Find Selected button in Button Group CallBack

14 views (last 30 days)
Alessandro Livi
Alessandro Livi on 6 Jul 2024 at 19:42
Commented: Voss on 7 Jul 2024 at 22:31
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
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];

Accepted Answer

Voss
Voss on 6 Jul 2024 at 23:29
function ManualRewardsButtonGroupSelectionChanged(app, event)
selectedButton = app.ManualRewardsButtonGroup.SelectedObject;
switch selectedButton
case app.Rew_ALeftButton
% Give LA reward
case app.Rew_BLeftButton
% Give LB reward
case app.Rew_ARightButton
% Give RA reward
case app.Rew_BRightButton
% Give RB reward
end
end
or
function ManualRewardsButtonGroupSelectionChanged(app, event)
switch event.NewValue
case app.Rew_ALeftButton
% Give LA reward
case app.Rew_BLeftButton
% Give LB reward
case app.Rew_ARightButton
% Give RA reward
case app.Rew_BRightButton
% Give RB reward
end
end
  2 Comments
Alessandro Livi
Alessandro Livi on 7 Jul 2024 at 21:52
Thanks, that fixed it. so the Value is a red herring, the Radio buttons do have numbers! TMI I missed that at the top
selectedButton =
RadioButton (2) with properties:
Value: 1
Text: 'Rew_A Right'
Position: [101 29 80 22]
This one can close
Voss
Voss on 7 Jul 2024 at 22:31
You're welcome! Any questions, let me know. Otherwise, please "Accept" this answer. Thanks!

Sign in to comment.

More Answers (0)

Products


Release

R2023b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!