How to change automatically generated uibutton properties in another function?

조회 수: 1 (최근 30일)
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 CenterFile Exchange에서 Startup and Shutdown에 대해 자세히 알아보기

제품


릴리스

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by