MATLAB Answers

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

33 views (last 30 days)
Travis Ohlsen
Travis Ohlsen on 11 Apr 2018
Commented: Walter Roberson on 12 Apr 2018
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 Comments

Rik
Rik on 11 Apr 2018
Have you read the documentation for uicontrol? What you are looking for is a callback. The question is also where you want that edited variable to be available, because that callback function will have its own workspace.
Travis Ohlsen
Travis Ohlsen on 12 Apr 2018
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)

Sign in to comment.

Answers (1)

Walter Roberson
Walter Roberson on 11 Apr 2018
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 Comments

Travis Ohlsen
Travis Ohlsen on 12 Apr 2018
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 on 12 Apr 2018
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.)

Sign in to comment.

Categories

Tags