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.