Choose a random button from a set of 9 arranged in a 3x3?

1 回表示 (過去 30 日間)
Myles
Myles 2024 年 4 月 21 日
コメント済み: Voss 2024 年 4 月 21 日
app.FirstNumber.Text = num2str(app.First_Number);
app.SecondNumber.Text = num2str(app.Second_Number);
app.Operator.Text = "+";
Answer = app.First_Number + app.Second_Number; % Operator needs to be able to change
n = randi(9);
app.Button_(n).Text = num2str(Answer); % Button needs to move to a random button_(1-9)
Essentially im trying to make a random button become the correct answer, but i keep getting an error message of Button_ is not a recodnized method or property.

回答 (1 件)

Voss
Voss 2024 年 4 月 21 日
Use this syntax:
button_name = sprintf('Button_%d',n);
app.(button_name).Text = num2str(Answer);
  1 件のコメント
Voss
Voss 2024 年 4 月 21 日
And to be able to change the operator, you can do something like this:
operator_lookup = { ...
"+", @plus; ...
"-", @minus; ...
"*", @times; ... actually element-wise multiplication, .*
"/", @rdivide; ... element-wise division, ./
};
idx = find([operator_lookup{:,1}] == string(app.Operator.Text), 1);
if isempty(idx)
error('Invalid operator')
end
Answer = operator_lookup{idx,2}(app.First_Number,app.Second_Number);
References:

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

カテゴリ

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

タグ

製品


リリース

R2024a

Community Treasure Hunt

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

Start Hunting!

Translated by