How do I define a pushbutton to change the value of a variable?

11 次查看(过去 30 天)
For a class project,I am having trouble using the uicontrol feature. I want to have a push button that changes the value of a variable when pressed. I first set the variable exe=1 and I want to set exe=2 when the button is pressed. What I have is listed below.
exe=1;
main_menu=uicontrol('Style','pushbutton','Position',[20 20 100 100],'String','Pong');
while %%pushbutton=down%%
close
exe=2;
end
If anyone can please help me with this, that would be wonderful. I have spent countless hours trying different things that people say to do with no luck. I have even copied and pasted things into matlab to see what results they find, and I only get error messages. If it matters, I currently have Matlab2018a.
  3 个评论
Travis Ohlsen
Travis Ohlsen 2018-4-12
编辑:Walter Roberson 2018-4-12
So how would you suggest I code the following logical statement
exe=1
while exe=1
create four push buttons with titles push1, push2, push3, push4 at different positions with their names displayed upon them
if push1 is pressed
close window and set exe=2
if push2 is pressed
close window and set exe=3
if push3 is pressed
close window and set exe=4
if push4 is pressed
close window and set exe=0
if no button is pressed
wait
while exe=2
(Code for game 1 here)
while exe=3
(Code for game 2 here)
while exe=4
(Code for game 3 here)
(When exe=0, script ends)

请先登录,再进行评论。

回答(1 个)

Walter Roberson
Walter Roberson 2018-4-11
When you use a uicontrol pushbutton, the Value associated with the button becomes 1 only until the end of the Callback associated with the control. Once the Callback has run, the Value property automatically returns to 1.
You are trying to query the control value outside of any callback, so the Value will always have returned to 0 by the time you read it.
This is different from style togglebutton, which stays at the new value until it is changed again by user action or by the program.
You should be using a callback. And you should be looking at http://matlab.wikia.com/wiki/FAQ#How_can_I_share_data_between_callback_functions_in_my_GUI.28s.29.3F to see how to change a variable in a callback.
  2 个评论
Travis Ohlsen
Travis Ohlsen 2018-4-12
编辑:Walter Roberson 2018-4-12
So how would you suggest I code the following logical statement
exe=1
while exe=1 create four push buttons with titles push1, push2, push3, push4 at different positions with their names displayed upon them if push1 is pressed close window and set exe=2 if push2 is pressed close window and set exe=3 if push3 is pressed close window and set exe=4 if push4 is pressed close window and set exe=0 if no button is pressed wait while exe=2 (Code for game 1 here) while exe=3 (Code for game 2 here) while exe=4 (Code for game 3 here)
(When exe=0, script ends)
Walter Roberson
Walter Roberson 2018-4-12
exe = 1;
while exe == 1;
exe = menu('', 'push1', 'push2', 'push3', 'push4');
switch exe
case 1:
exe = 2;
case 2:
exe = 3;
case 3:
exe = 4;
case 4:
exe = 0;
otherwise
exe = 1;
end
while exe == 2
%code for game 1 here
end
while exe == 3
%code for game 2 here
end
while exe == 4
%code for game 3 here
end
if exe == 0
break; %redundant since 0 is not 1 so while would end anyhow
end
end
A glance at this code would show you that there is no way to leave the games once they have started, since the games are not permitted to change exe (because they are not documented as doing so.)

请先登录,再进行评论。

类别

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