When using checkbox, how do I cancel the last check?
10 次查看(过去 30 天)
显示 更早的评论
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 个评论
回答(1 个)
Deepak
2024-11-15,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 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Data Type Conversion 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!