I get these errors when I run this code and need help finding a solution.

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 dir
Function 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 char
Conversion 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);
end
end
Thank you.

2 Comments

UI controls are not convertible to strings
but the tree node should have a property with the string you need
I'm guessing it's Text, but you should have a look at the documentation
Yes the uitreenode property is set to Text in the code, as stated in thhe documentation, but I am getting an error.

Sign in to comment.

 Accepted Answer

Yes the uitreenode property is set to Text
by that do you mean this line of code?
nodes(k)=uitreenode(t, 'Text', FolderName);
If you ment this line, it sets the text of the tree node to be the folder name
Your problem is here:
files=dir(node);
where you send the tree node as an argument to dir function, while dir expects a character array, hence the error.
what you need to do is this:
files=dir(node.Text);
after that you will still have some more issues of setting the callbacks of the new nodes, duplication of the tree when reselecting a folder both of which you don't handle in the callback function

6 Comments

Yes I had meant the first line code. I changed node to node.Text it worked, but I then realized the issues you just mentioned. In order for the callback function to work on new nodes am i supposed to make it recursive?
Thank you!
Yes You Need To Register The Callback Of Each New Tree Node You Add.
I tried adding: t.SelectionChangedFcn=@nodechange; to the subfolder for loop, but it didnt work. Is there another way to call the function for each new node?
I was obviously wrong... SelectionChangedFcn is a property of the tree...
There were a few bugs there...
repopulation of a node that was already populated, the folder name was used in dir function instead of the entire folder path, and finally the new nodes were added to the tree instead of to the appropriate parent treenode
I cleaned it all up, it should work properly now...
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)
node=eventdata.SelectedNodes;
% stop execution of node is already populated
if ~isempty(node.Children)
return;
end
% Get a list of all files and folders in this folder.
files = dir(getNodePath(node));
%Get a logical vector that tells which is a directory, exclude the '.' and '..' directories
dirFlags = [files.isdir] & cellfun(@(a) ~any(regexp(a, '^\.{1,2}$')), {files.name});
%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(node, 'Text', FolderName);
end
expand(node);
end
function path = getNodePath(node)
% this gets the full path of a tree-node folder
if isa(node.Parent, 'matlab.ui.container.Tree')
path = node.Text;
return;
end
path = fullfile(getNodePath(node.Parent), node.Text);
end
Wow it is perfect!! Makes so much sense to have to provide the path of the folder and not just the name and to add the new nodes to the node and not the tree. Just had to add a vetor and loop to display the files as well. Thank you very much!! Appreciate it!

Sign in to comment.

More Answers (0)

Categories

Asked:

on 8 Jan 2019

Commented:

on 21 Jan 2019

Community Treasure Hunt

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

Start Hunting!