When using checkbox, how do I cancel the last check?
11 views (last 30 days)
Show older comments
checkbox3_value=get(handles.checkbox3, 'Value');
if checkbox3_value == 1
a = [0:10]';
b = num2str(a); c = cellstr(b);
text(Nodelocation(:,2), Nodelocation(:,3), c);
else
end
How to not display text when else?
I tried delete and visible but it just doesn't work.
0 Comments
Answers (1)
Deepak
on 15 Nov 2024 at 11:33
This issue can be resolved by implementing a “persistent” variable to store the handles of the text objects, allowing for efficient management of their visibility. When the checkbox is “checked”, the text is created, and its handles are stored. If the checkbox is “unchecked”, the stored text handles are used to delete the text objects, ensuring they are removed from the plot.
This approach ensures that text annotations are only displayed when needed and are reliably cleared when not, maintaining a clean and responsive user interface.
Here is the sample MATLAB code to achieve the same:
% Create a figure
hFig = figure('Position', [100, 100, 400, 300]);
% Create an axes
hAxes = axes('Parent', hFig, 'Position', [0.1, 0.3, 0.8, 0.6]);
% Create a checkbox
hCheckbox = uicontrol('Style', 'checkbox', 'String', 'Show Text', ...
'Position', [150, 50, 100, 30], ...
'Callback', @checkboxCallback);
% Sample Nodelocation data
Nodelocation = [0:10; 0:10; 0:10]';
% Store Nodelocation in the figure's app data
setappdata(hFig, 'Nodelocation', Nodelocation);
% Callback function for the checkbox
function checkboxCallback(hObject, ~)
checkbox3_value = get(hObject, 'Value');
% Access Nodelocation from the figure's app data
Nodelocation = getappdata(hFig, 'Nodelocation');
persistent textHandles;
if checkbox3_value == 1
a = [0:10]';
b = num2str(a);
c = cellstr(b);
% Display text and store the handles
textHandles = text(Nodelocation(:,2), Nodelocation(:,3), c, 'Parent', hAxes);
else
% Check if textHandles is not empty and delete the text objects
if ~isempty(textHandles)
delete(textHandles);
textHandles = [];
end
end
end
For more information on the functions used refer to the following documentation:
I hope this helps in resolving the issue.
0 Comments
See Also
Categories
Find more on Data Type Conversion in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!