How to change automatically generated uibutton properties in another function?

1 回表示 (過去 30 日間)
sid
sid 2021 年 11 月 22 日
コメント済み: sid 2021 年 11 月 24 日
The following code is run at startup that generated buttons based on a number of unique files in a folder. The callbackfcn is another function, where the user would choose one of these buttons. At that point, i'd like to highlight the chosen button, which is easy, but i would also like to grey out, or delete the other buttons. The qquestion is how to I access the other buttons that were generated?! I can get to the button that was clicked easily, just not the other ones.
%
% make buttons
numButtons = max(size(app.orientationUnique));
for b = 1:numButtons
posidx = b*(30+app.TheHuntGUI.Position(1));
app.orientationButtons = uibutton(app.TheHuntGUI, 'push');
app.orientationButtons.FontName = 'Arial';
app.orientationButtons.FontSize = 18;
app.orientationButtons.FontWeight = 'bold';
app.orientationButtons.Position = [posidx 500 120 40];
app.orientationButtons.Text = app.orientationUnique{b};
app.orientationButtons.Tag = app.orientationUnique{b};
app.orientationButtons.ButtonPushedFcn = createCallbackFcn(app, @orientationButtonPushed, true);
end
  1 件のコメント
sid
sid 2021 年 11 月 23 日
Resolved.
numButtons = max(size(app.orientationUnique));
for b = 1:numButtons
posidx = b*(30+app.TheHuntGUI.Position(1));
app.orientationButtons(b) = uibutton(app.TheHuntGUI, 'push');
set(app.orientationButtons(b),'FontName','Arial');
set(app.orientationButtons(b),'FontSize',18);
set(app.orientationButtons(b),'FontWeight','bold');
set(app.orientationButtons(b),'Position',[posidx 500 120 40]);
set(app.orientationButtons(b),'Text', app.orientationUnique{b});
set(app.orientationButtons(b),'Tag',app.orientationUnique{b});
set(app.orientationButtons(b),'ButtonPushedFcn',createCallbackFcn(app, @orientationButtonPushed, true));
end

サインインしてコメントする。

採用された回答

Mohammad Sami
Mohammad Sami 2021 年 11 月 22 日
You can store all your buttons as an array in the app property orientationButtons.
function create_buttons(app)
% % make buttons
numButtons = max(size(app.orientationUnique));
for b = 1:numButtons
posidx = b*(30+app.TheHuntGUI.Position(1));
app.orientationButtons(b) = uibutton(app.TheHuntGUI, 'push');
app.orientationButtons(b).FontName = 'Arial';
app.orientationButtons(b).FontSize = 18;
app.orientationButtons(b).FontWeight = 'bold';
app.orientationButtons(b).Position = [posidx 500 120 40];
app.orientationButtons(b).Text = app.orientationUnique{b};
app.orientationButtons(b).Tag = app.orientationUnique{b};
app.orientationButtons(b).ButtonPushedFcn = createCallbackFcn(app, @orientationButtonPushed, true);
end
end
function orientationButtonPushed(app,event)
btnclicked = event.Source;
allotherbtns = app.orientationButtons(~ismember(app.orientationButtons,btnclicked));
end
Also I suggest you use uigridlayout as the parent for your buttons. This will automatically adjust the sizes of the button based on the available space instead of manually setting the positions in code.
  3 件のコメント
Mohammad Sami
Mohammad Sami 2021 年 11 月 23 日
How did you access the fontname property. You need to but the index before the .FontName.
app.orientationButtons(b).FontName = 'Arial';
sid
sid 2021 年 11 月 24 日
resolved. cant get your solution to work, but the uigridlayout is a good idea. thank you.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeStartup and Shutdown についてさらに検索

製品


リリース

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by