How to save and load CheckedNodes of checkbox tree?

28 visualizzazioni (ultimi 30 giorni)
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.

Risposta accettata

Paras Gupta
Paras Gupta il 15 Set 2023
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.

Più risposte (0)

Categorie

Scopri di più su Develop uifigure-Based Apps in Help Center e File Exchange

Prodotti


Release

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by