MATLAB Answers

How can I fix my uicontrol script?

20 views (last 30 days)
I am currently creating a program for a class project and am having trouble using the uicontrol. For this part of the project, I am using while loops to change between scripts based on what value is assigned to the variable exe. I first set exe=1 and then run the while loop; while exe==1. Inside the while loop, I want the script to create a figure with a push button. What I want is for the script to wait for the button to be pressed, and then once it is pressed, I want the window to close, and to set exe=2 so that the next part of the script will run.
Here is what I have for a script. The while loops work, but the script doesn't like the callback.
exe=1;
while exe==1
main_menu=uicontrol('Style','pushbutton','Position',[20 20 20 20],'String','Pong','Callback',@exe1);
while get(handles.main_menu,'Value')==1
close
exe=2;
end
end
function exe1
get(handles.main_menu,'Value');
end
The result is the figure popping up with the push button along with the error;
Undefined variable "handles" or class "handles.main_menu".
When I push the button is returns another error;
Undefined function 'exe1' for input arguments of type 'matlab.ui.control.UIControl'.
Error while evaluating UIControl Callback.

  0 Comments

Sign in to comment.

Accepted Answer

Prajit T R
Prajit T R on 12 Apr 2018
Hi Travis
The following code should do the trick.
global exe;
exe=1;
main_menu=uicontrol('Style','pushbutton','Position',[60 60 60 60],'String','Pong','Callback',@pushbutton_callback);
function pushbutton_callback(src,event)
global exe;
exe=exe+1;
close;
end
Cheers

  3 Comments

Travis Ohlsen
Travis Ohlsen on 12 Apr 2018
Thanks for your help, but I still have a problem. Here is my main script:
%%%%%Begining of Script %%%%%
global exe
exe=1;
while exe==1
main_menu=uicontrol('Style','togglebutton','Position',[20 20 100 100],'String','Pong','Callback',@setexe2);
end
while exe==2
disp(exe)
end
%%%%%End of Script %%%%%
and here is my function script saved as setexe2.m:
function setexe2 (src,event)
global exe
exe=2;
close
end
What happens is when I run my script, nothing happens. I then stop the scrip by pressing ctrl+c and then the figure appears. The pushbutton does seem to work as when I press it, it does set exe=2 and closes the window. However, because the script is stopped, it does not switch to the next while loop and display the value of exe.
Please let me know if you have any more advice.
Travis Ohlsen
Travis Ohlsen on 12 Apr 2018
Never Mind, I just added a break after the uicontrol and it fixed the problem. Thank you for your help.
Walter Roberson
Walter Roberson on 12 Apr 2018
The code
while exe==1
main_menu=uicontrol('Style','togglebutton','Position',[20 20 100 100],'String','Pong','Callback',@setexe2);
end
means to loop indefinitely, and each iteration create a new uicontrol. The loop does not give any time for the graphics to be updated, and it adds all of the uicontrols onto the same position.

Sign in to comment.

More Answers (0)

Tags