Button press emulation matlab

Button press

2 件のコメント

Walter Roberson
Walter Roberson 2022 年 4 月 5 日
Do you need it to emulate clicking on a control that you have created, and no decoration is fine (activity is just triggered)? Or does it need to emulate mouse tracking and visible clicking on a control that you have created? Or does it need to emulate typing within the things you have designed? Or does it need to be able to interact with elements outside of what you have designed (e.g., controlling an external tetris game) ?
Adam Danz
Adam Danz 2022 年 4 月 6 日
@Namita Gera, in response to your PM, I would recommend what @Walter Roberson describes in the answer below. Pressing a button evokes the button's callback function. So each button should be assigned a callback function. You could either assign a different callback fcn to each button or assign the same callback fcn to all of the buttons and use inputs to decide which button was pressed.
There is an app testing framework that can simulate button presses but it would be much easier and more efficient to call the callback function directly. If you wanted to visually simulate the button-press so the user sees which button was selected, you could temporarily change the button color for 0.2 sec or so.

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

回答 (1 件)

Walter Roberson
Walter Roberson 2022 年 4 月 5 日

1 投票

Each button has an associated Callback property. To emulate pushing a button, have the program create a fake event structure (if needed) and call the stored Callback .

8 件のコメント

Walter Roberson
Walter Roberson 2022 年 4 月 6 日
app.Button_(n).Text = "X";
That kind of approach can work, provided that you defined a property named Button_ and assigned to it appropriate button controls. One way to do that would have been to have already created app.Button_11, App.Button_12, App.Button_13, App.Button_21 and so on, and then
app.Button_ = [app.Button_11, App.Button_12, App.Button_13, App.Button_21 and so on]
The problem with your
n = randi(9)
is that you are not taking into account the ones that are already disabled. But you can
eligible = find(ismember("on", [app.Button_.Enable]));
n = eligible(randi(length(eligible)));
app.Button_(n) etc.
Walter Roberson
Walter Roberson 2022 年 4 月 7 日
eligible = find(ismember([app.Button_.Enable], "on"));
I am not clear as to why you are setting the text of Button_1 to X ? Are you always letting the computer go first, and having it always take the same first move ?
If so your button numbering seems a bit odd: why would Button_1 be in the middle of Button_65 and so on?
I would suggest to you that it would make the most sense to use grid indexing for your button identifiers, Button_RC like Button_32 for row 3 column 2.
Walter Roberson
Walter Roberson 2022 年 4 月 7 日
app.Button_ = [app.Button_65, app.Button_66, app.Button_67, app.Button_68,
app.Button_69, app.Button_70, app.Button_71, app.Button_72]
Note that only has to be done once, after you have created the buttons. The handles of your buttons are not going to change during game play.
Walter Roberson
Walter Roberson 2022 年 4 月 8 日
Set app.Button_ to the list of handles app_Button_65 to app_Button_128 .
This might be a bit tedious if you created all of those buttons ahead of time in App Designer, but if you created the buttons at run time then it should be fairly easy.
Namita Gera
Namita Gera 2022 年 4 月 8 日
I have done this but it seems to only press the first button in the app.Button_ list.
Walter Roberson
Walter Roberson 2022 年 4 月 8 日
Well, try debugging it with
eligible = find(ismember("on", [app.Button_.Enable]));
num_available = length(eligible)
choice = randi(num_available)
n = eligible(choice);
and seeing how many are said to be available and what the random choice is.
Is it possible that when you created the buttons, you did not se them as Enable "on" ?
Walter Roberson
Walter Roberson 2022 年 4 月 8 日
eligible = find(ismember[app.Button_.Enable], "on"));
Namita Gera
Namita Gera 2022 年 4 月 11 日
Thank you very much worked in the end.

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

カテゴリ

ヘルプ センター および File ExchangeJust for fun についてさらに検索

製品

タグ

質問済み:

2022 年 4 月 5 日

編集済み:

2022 年 4 月 12 日

Community Treasure Hunt

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

Start Hunting!

Translated by