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

조회 수: 11 (최근 30일)
Travis Ohlsen
Travis Ohlsen 2018년 4월 11일
댓글: Walter Roberson 2018년 4월 12일
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)
Walter Roberson
Walter Roberson 2018년 4월 12일
menu('Choose', 'push1', 'push2', 'push3', 'push4')

댓글을 달려면 로그인하십시오.

답변 (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