I need help fix a check box in a gui

3 次查看(过去 30 天)
Amanda
Amanda 2022-12-2
回答: Dev 2025-4-29
I have create a gui for a robot maze. When the robot moves through the maze a blue line follows the path the robot took. In my gui I have a check box that is label history when it checked i want it to show the blue line when its unchecked I want it to no show the blue line. I am unsure how to do this.
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 sender.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 个)

Dev
Dev 2025-4-29
We can show/hide the blue line based on the current state of the check box by leveraging the steps below-
  • Store a handle to the blue line when you plot it: We can use the “setappdata” function in MATLAB to store the blue line and the “getappdata” function to share this handle among callbacks.
Please refer to an example code snippet below-
figureHandle = gcf;
% After plotting the blue line, store its handle:
blueLine = plot(xPath, yPath, 'b', 'LineWidth', 2); % Example plotting
setappdata(figureHandle, 'blueLineHandle', blueLine);
  • Update the “toggleHistory” function: The “toggleHistory” function in the code provided in the question, we can show/hide the line by setting its ‘Visible’ property. The code provided below achieves the same-
fig = ancestor(sender, 'figure');
blueLine = getappdata(fig, 'blueLineHandle');
if isempty(blueLine) || ~isgraphics(blueLine)
return; % No line to show/hide yet
end
if sender.Value == 1
set(blueLine, 'Visible', 'on');
else
set(blueLine, 'Visible', 'off');
end
For more information regarding the usage of the functions mentioned above, kindly refer to the documentation links below-
I hope the above steps help solve your query.

类别

Help CenterFile Exchange 中查找有关 Entering Commands 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by