How use a check box in a GUI

3 次查看(过去 30 天)
Amanda
Amanda 2022-12-2
I have created a Gui for a robot maze. I have a check box that shows the togglehistory its checked and doesnt show it when its unchecked. I have wrote a function for this but it does do anything when I click the but other than displaying show history or hide history. I'm not sure how to make the check box enable and disable the toggle history.
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)
if history.Value == 1
disp showHistory
else
disp 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

回答(1 个)

Eric Delgado
Eric Delgado 2022-12-3
编辑:Eric Delgado 2022-12-3
First of all, you have to save your "toggleHistory" function in a file "toggleHistory.m". And then you have to replace "history.Value" for "sender.Value" (because your function doesn't know who is "history"). Hope it helps! :)
Hummmm... final tip. Go to App Designer enviroment.
% File "toggleHistory.m"
function toggleHistory(sender, eventdata)
if sender.Value == 1
disp("showHistory")
else
disp("hideHistory")
end
end

类别

Help CenterFile Exchange 中查找有关 Interactive Control and Callbacks 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by