How to save and load CheckedNodes of checkbox tree?

12 次查看(过去 30 天)
Hi,
I have generated uifigure and added a checkbox tree. I would like users to start with the last state of the checkbox when they open the uifigure. How can I save the checkbox status (CheckedNodes) and load it as an initial state when the next use?
Thank you for your help in advance.

采纳的回答

Paras Gupta
Paras Gupta 2023-9-15
Hi,
I understand that you want to save the current checkbox states and load them when the user opens the UI Figure next time. We can achieve the same by saving the checkbox data into a MAT file and loading them again on startup. The code given below illustrates the same on a sample CheckBox Tree.
% Create the UI figure and tree
fig = uifigure;
t = uitree(fig,'checkbox');
% Create the parent and child nodes
parent = uitreenode(t,'Text','Sample Data');
child = uitreenode(parent,'Text','Sample 1');
% Expand the tree
expand(t)
% Add some more nodes
child2 = uitreenode(parent,'Text','Sample 2');
grandchild = uitreenode(child2,'Text','Subsample 1');
grandchild2 = uitreenode(child2,'Text','Subsample 2');
% Create a list separately for nodes and their names
nodes = [parent child child2 grandchild grandchild2];
nodes_name = {parent.Text, child.Text child2.Text grandchild.Text grandchild2.Text};
% Load the previous state of the checkbox using the function defined at the
% bottom
loadCheckboxStatus(t, nodes_name, nodes);
% Create a save button
saveButton = uibutton(fig, 'Text', 'Save', 'Position', [10 10 100 30]);
% Callback function for the save button
saveButton.ButtonPushedFcn = @(src, event) saveCheckboxStatus(t);
% Create a load button
loadButton = uibutton(fig, 'Text', 'Load', 'Position', [120 10 100 30]);
% Callback function for the load button
loadButton.ButtonPushedFcn = @(src, event) loadCheckboxStatus(t, nodes_name, nodes);
% Function to save the checkbox status
function saveCheckboxStatus(tree)
% Save the checkbox status (CheckedNodes) to a MAT file
checkboxStatus = tree.CheckedNodes;
save('checkbox_status.mat', 'checkboxStatus');
end
% Function to load the checkbox status
function loadCheckboxStatus(tree, nodes_name, nodes)
% Check if the checkbox_status.mat file exists
if exist('checkbox_status.mat', 'file') == 2
% Load the saved checkbox status from the MAT file
loadedData = load('checkbox_status.mat');
% Retrieve the nodes that were checked previously
checkedNodes = [];
for i = 1:numel(loadedData.checkboxStatus)
temp = loadedData.checkboxStatus(i).Text;
if ismember({temp}, nodes_name)
% Find the index of the retrieved node in the nodes list
[~, index] = ismember({temp}, nodes_name);
% Append the node to checkedNodes
checkedNodes = [checkedNodes, nodes(index)];
end
end
% Set the CheckedNodes property of the UI Tree
tree.CheckedNodes = checkedNodes;
end
end
Please refer to the following documentations for more information on the functions used in the code:
Hope this helps.

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Develop uifigure-Based Apps 的更多信息

产品


版本

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by