How to fix a callback for a checkbox

7 views (last 30 days)
Amanda
Amanda on 4 Dec 2022
Edited: Rik on 4 Dec 2022
I have a check box that tracks the toggle history of my robot meaning when its clicked its shows the path of the robot and when its unclicked it doesnt. I have write a fucntion but Idk why its wont work when I click the button.
createRandomMaze
figure;
first_button = uicontrol('style', 'pushbutton');
set(first_button, 'units', 'normalized', 'position', [0.2,0.7, 0.2, 0.2])
set(first_button, 'string', 'turn left')
set(first_button, 'callback', @turnLeft)
second_button = uicontrol('style', 'pushbutton');
set(second_button, 'units', 'normalized', 'position', [0.4, 0.7, 0.2, 0.2])
set(second_button, 'string', 'step')
set(second_button, 'callback', @step)
third_button = uicontrol('style', 'pushbutton');
set(third_button, 'units', 'normalized', 'position', [0.6, 0.7, 0.2, 0.2])
set(third_button, 'string', 'turn right')
set(third_button, 'callback', @turnRight)
fourth_button = uicontrol('style', 'pushbutton');
set(fourth_button, 'units', 'normalized', 'position', [0.2, 0.5, 0.2, 0.2])
set(fourth_button, 'string', 'left')
set(fourth_button, 'callback', @stepLeft)
fifth_button = uicontrol('style', 'pushbutton');
set(fifth_button, 'units', 'normalized', 'position', [0.4, 0.5, 0.2, 0.2])
set(fifth_button, 'string', 'back')
set(fifth_button, 'callback', @stepBack)
sixth_button = uicontrol('style', 'pushbutton');
set(sixth_button, 'units', 'normalized', 'position', [0.6, 0.5, 0.2, 0.2])
set(sixth_button, 'string', 'right')
set(sixth_button, 'callback',@stepRight)
seventh_button = uicontrol ('style','checkbox');
set(seventh_button, 'units', 'normalized', 'position', [0.2, 0.3, 0.2, 0.2])
set(seventh_button, 'string', 'History')
set(seventh_button, 'callback', @toggleHistory)
eighth_button = uicontrol ('style', 'edit');
set(eighth_button, 'units', 'normalized', 'position', [0.4, 0.37, 0.2, 0.05])
set(eighth_button, 'string', '2')
ninth_button = uicontrol('style','pushbutton');
set(ninth_button, 'units', 'normalized', 'position', [0.6, 0.3, 0.2, 0.2])
set(ninth_button, 'string', 'Run!')
set(ninth_button, 'callback', @Run)
ninth_button.Callback = 'number_of_steps = str2num(eighth_button.String); walk(number_of_steps)';
function toggleHistory(sender, eventdata)
History = (['seventh_button', 'value']);
if History
showhistory
else ~History
hidehistory
end
end
function Run(sender, eventdata)
step_input = (['eighth_button', 'string']);
if ~strcmp(step_input, ' ');
number_of_steps = str2num(step_input);
walk(number_of_steps);
end
end

Answers (1)

Rik
Rik on 4 Dec 2022
Perhaps you should have a read here.
You probably intend to extract the value property, but that requires this:
History = sender.Value;
You also don't need to invert a statement for an else, only for an elseif.
If you want access to the variables you set up, you will have to use guidata, or use nested functions.
  2 Comments
Amanda
Amanda on 4 Dec 2022
How would i you a nested function
Rik
Rik on 4 Dec 2022
Edited: Rik on 4 Dec 2022
This should not be hard to find. Where have you tried to find the answer yourself?
Did you follow a basic Matlab tutorial? Did you Google this?
Also, why didn't you ask a follow up question in the previous thread about this? You should either mark an answer as accepted, or ask a follow up question until the issue is resolved. This duplication of questions is a waste of a very finite resource. There are not many people who are willing to spend a lot of time helping people on this forum.

Sign in to comment.

Categories

Find more on Interactive Control and Callbacks in Help Center and File Exchange

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!