Hello, the code below is supposed to display a selected folder in a graphical tree and then open its subfolders and files. However, when I run it, it gives me the following errors related to dir and callback function:
Error using dirFunction is not defined for 'matlab.ui.container.TreeNode' inputs.Error in mytreeapp/nodechange (line 14)files=dir(node); Error using matlab.ui.control.internal.controller.ComponentController/executeUserCallback (line 352)Error while evaluating Tree PrivateSelectionChangedFcn.The input of dir needs to be a string so i tried converting the root using char but I got this error:Error using charConversion to char from matlab.ui.container.TreeNode is not possible.
I would appreciate it if someone can help me find a solution to these errors.
f=uifigure; % Figure
t=uitree(f, 'Position', [20 20 150 150]); %Tree
t.SelectionChangedFcn=@nodechange;%Select the folder using uigetdir and set it as the root node of the tree
root=uitreenode(t, 'Text', uigetdir);%Callback function when node is selected
function nodechange (t, eventdata, handles)node=eventdata.SelectedNodes;% Get a list of all files and folders in this folder.
files=dir(node);%Get a logical vector that tells which is a directory
dirFlags=[files.isdir];%Extract only those that are directories
subFolders=files(dirFlags);%Loop to display every subfolder as a node
for k=1: length (subFolders);FolderName=subFolders(k).name;nodes(k)=uitreenode(t, 'Text', FolderName);endend
Thank you.
Best Answer