App Designer- How to make button display answers in different numeric fields depending on the clicks

25 visualizaciones (últimos 30 días)
Hello, Im currently designing a game in App designer where you can click a button and it displays a random number between 1-10. Currently in my app, when you click the "roll" button it generates 4 different numbers into 4 different text fields and I don't want that. I want the button when clicked once it displays one random generated number in the first text box and when I click roll again it displays another number in the second box and etc... for the next two clicks. Heres an example of my code below:
function RollValueChanged(app, event) %Roll is the name of the button
FirstValue= randi([1, 10],1,1);
app.FirstText.Value= FirstValue; %FirstText is the numeric field text box that the answer will be shown at.
SecondValue= randi([1, 10],1,1);
app.SecondText.Value= SecondValue; %SecondText is the numeric field text box that the answer will be shown at.
ThirdValue= randi([1, 10],1,1);
app.ThirdText.Value= ThirdValue; %ThirdText is the numeric field text box that the answer will be shown at.
FourthValue= randi([1, 10],1,1);
app.FourthText.Value= FourthValue; %FourthText is the numeric field text box that the answer will be shown at.
  4 comentarios
Mohammad Sami
Mohammad Sami el 24 de Nov. de 2020
I meant what you want to happen to the first value when you click the second time. So for the second time onwards, do you want to keep the values generated previously ? What should happen when you click the 5th time ?
Sean
Sean el 24 de Nov. de 2020
I want the first value to remain the same and yeah after the second time onwards it keeps the previously generated number. Also when you click the button 5 or times nothing will happen. You can only click four times to generate the four numbers.

Iniciar sesión para comentar.

Respuestas (1)

Mohammad Sami
Mohammad Sami el 25 de Nov. de 2020
Something like this should work. it assumes the initial values are set to 0.
function RollValueChanged(app, event) %Roll is the name of the button
Value= randi([1, 10],1,1);
switch true
case app.FirstText.Value == 0
app.FirstText.Value = Value;
case app.SecondText.Value == 0
app.SecondText.Value = Value;
case app.ThirdText.Value == 0
app.ThirdText.Value = Value;
case app.FourthText.Value == 0
app.FourthText.Value = Value;
otherwise
% do nothing
end
end
  2 comentarios
Sean
Sean el 27 de Nov. de 2020
Can you explain why we would use a switch here and why set the switch to true? The function works, but I don't understand why it works. Thank you in advance!
Mohammad Sami
Mohammad Sami el 27 de Nov. de 2020
It's not necessary to do it this way. You can essentially do the same with if else statements.
The switch statement will evaluate the case statements and it will execute the one that is true. If there are multiple cases that are true, it will only execute the first one that is true.
There is some discussion on this topic on the thread here. You can see if you want to implement this with if else.
https://www.mathworks.com/matlabcentral/answers/12290-problem-using-switch-with-logical-operators-larger-than-do-this-or-less-than-do-this
There are still other ways to implement this besides the above 2.

Iniciar sesión para comentar.

Categorías

Más información sobre Graphics Object Programming en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by