How to open a window after pushing a button

I could not find the right answer after a long search.
This is my script:
%% Create a figure window
fig=uifigure();
%% Create the 'PLAY GAME' push button
play_game = uibutton(fig,'push');
play_game.Text = 'PLAY GAME';
play_game.Position = [100, 490, 170, 50];
How can I open another window after pushing the 'play game' button?
Thanks

回答 (1 件)

Adam Danz
Adam Danz 2020 年 10 月 18 日
編集済み: Adam Danz 2020 年 10 月 21 日

0 投票

Check out the description for the ButtonPushedFcn in uibuttons.
play_game.ButtonPushedFcn = @(src, event)myPlaybackFunction(src, event)
function myPlaybackFunction(src, event)
% do stuff
end

2 件のコメント

Adam Danz
Adam Danz 2020 年 10 月 21 日
Cecilia Geroldi's answer moved here as a comment
I've done this:
play_game.ButtonPushedFcn = @(src, fig2)PlayGameFunction(src, fig2);
function PlayGameFunction(~,~)
% do stuff
fig2 = uifigure();
end
It works but I'm not sure if it is correct
Adam Danz
Adam Danz 2020 年 10 月 21 日
Are you doing this in App Designer? If so, you should pass in the app (play_game?) handle like this,
play_game.ButtonPushedFcn = @(src, fig2)PlayGameFunction(app, src, fig2);
function PlayGameFunction(app,~,~)
% do stuff
end
Your function is just creating a uifigure. If that's what you want the "play game" button to do, then the approach looks file, outside of app designer and assuming you don't need the two inputs.

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

カテゴリ

ヘルプ センター および File ExchangeApp Building についてさらに検索

製品

リリース

R2020a

タグ

質問済み:

2020 年 10 月 17 日

コメント済み:

2020 年 10 月 21 日

Community Treasure Hunt

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

Start Hunting!

Translated by