How do I define a pushbutton to change the value of a variable?
12 views (last 30 days)
Show older comments
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.
Answers (1)
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
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.)
See Also
Categories
Find more on Migrate GUIDE Apps in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!