Get index of selected button in button toggle group

5 ビュー (過去 30 日間)
Jim Hokanson
Jim Hokanson 2017 年 11 月 10 日
回答済み: Joshua Brownlow 2019 年 4 月 1 日
When using a ButtonGroup in AppDesigner it is not clear how to get the index of the selected button. The only available approach appears to be:
string_values = {h.Buttons.Text};
I = find(strcmp(h.SelectedObject.Text,string_values));
Is there anything more direct?

回答 (2 件)

Matt J
Matt J 2017 年 11 月 10 日
編集済み: Matt J 2017 年 11 月 10 日
I don't think so, but I don't immediately see why you would want to assign numeric indices to the different buttons. Processing the selection would normally be done index-free using a switch...case
switch h.SelectedObject.Text
case 'Button #1'
case 'Button #2'
end
If you want to associate selections with numeric values, a slider or spinner would be a more appropriate component.

Joshua Brownlow
Joshua Brownlow 2019 年 4 月 1 日
I still don't think this is the best way, but you can cycle through the buttons in the group with a for loop to check which index has a value of 1 for being pressed down
for i = 1:buttonGroupSize
app.ButtonGroup.Buttons(i).Value
end

カテゴリ

Help Center および File ExchangeFunction Creation についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by