Generating an unknown ammount of Buttons in App Designer

조회 수: 42 (최근 30일)
Ahmet Sahin
Ahmet Sahin 2019년 8월 26일
댓글: Myles 2024년 4월 21일 14:59
Hello,
I'm trying to generate Buttons programmatically with the App Designer.
I have an Excel File, where all the names of the Buttons are listet, not knowing how many Buttons (or lines in Excel) will be needed.
I figured out how to create components with the App Designer programmtically:
So I tried many things, but none of them worked.
The problem is, that (as far as I understood) every Button (or other element) in the GUI needs his own variable. So I have to create new variables with the function eval, wich is, how I found out on every site here, absoulutly forbidden...
My first thought was to create an Array, but it didn't work:
Unable to perform assignment because dot indexing is not supported for variables of this type.
And heres the Code:
A = [378 700 100 22]; %Position of the Button
allKeys = string(keys(CellContOne)); %allKeys gets all the Buttonnames as a String
UniButton = zeros(length(allKeys)); %An Array with the length of the List is created
for i = 1:length(allKeys) %For Loop to create the Buttons
UniButton(i) = uibutton(app.UIFigure, 'state'); %Creating a Button
UniButton(i).Position = A; %Giving the Button a position
UniButton(i).Text = allKeys(i); %Giving the Button it's Text
b_text = UniButton(i).Text; %global Variable gets the Text
b_value = UniButton(i).Value; %global Variable gets the Value
UniButton(i).ValueChangedFcn = @app.mybuttonpress; %Callback function
A(2) = A(2) -25; %Position change for the next Button
end
I also tried using just one variable for every Button, but it didn't work because the GUI wasn't able to figure out wich button was pressed (I need this information for the callback) The code looks the same, just without the index (i) and the line UniButton = zeros(length(allKeys));
Structures and Cell Arrays also didn't work for me.
I hope You can help me!
  댓글 수: 1
Myles
Myles 2024년 4월 21일 14:59
Annoyingly enough, im having a similar problem, but Button_ always comes up as being an unrecognized property, so it doesn't seem to work.

댓글을 달려면 로그인하십시오.

채택된 답변

xi
xi 2019년 8월 26일
You need to remove the line:
UniButton = zeros(length(allKeys));
In the callback function, you can retreive source.Text to determine which button is pressed and source.Value to determine the state of the pressed button:
function mybuttonpress(app,source,event)
source.Value
source.Text
end
  댓글 수: 1
Ahmet Sahin
Ahmet Sahin 2019년 8월 26일
Hi Xi,
thank You for Your help. I also didn't know about the source.___ thing, that also really helped me out.
Thank You!

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Dennis
Dennis 2019년 8월 26일
Hi,
your code should almost work, you need to remove the preallocation with zeros. You can let your loop run backwards instead:
A = [378 700 100 22]; %Position of the Button
allKeys = string(keys(CellContOne)); %allKeys gets all the Buttonnames as a String
for i = length(allKeys):-1:1 %For Loop to create the Buttons
UniButton(i) = uibutton(app.UIFigure, 'state'); %Creating a Button
UniButton(i).Position = A; %Giving the Button a position
UniButton(i).Text = allKeys(i); %Giving the Button it's Text
b_text = UniButton(i).Text; %global Variable gets the Text
b_value = UniButton(i).Value; %global Variable gets the Value
UniButton(i).ValueChangedFcn = @app.mybuttonpress; %Callback function
A(2) = A(2) -25; %Position change for the next Button
end
  댓글 수: 2
Ahmet Sahin
Ahmet Sahin 2019년 8월 26일
Hi Dennis,
thank you very much, now everything works fine!
Namita Gera
Namita Gera 2022년 4월 6일
I am struggling with the callback function on buttons and was hoping you could help.
I am creating a game in matlab app designer in which a player plays against the computer opponent. I want to code the CPU to press a button at random when it is its turn. For example, in TicTacToe the player plays against another player but in this case, the opponent is the CPU. The CPU is able to click buttons at random for example if there are 9 buttons it will press on any of those 9 randomly providing it has not been pressed already. I am not sure how to program this any help would be highly appreciated.
This is what i have so far
if app.pl_move == 2
n = randi(9)
app.Button_(n).Text = "X";
app.Button_(n).Enable = "off";
app.pl_move = 1;
end

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Migrate GUIDE Apps에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by