请注意,树中的所有内容都是节点,包括文件夹和图像.您需要在选择回调中执行检查以测试所选节点是否是文件夹.
The currently-selected node(s) can be accessed using mtree.getSelectedNodes. Node selection
callbacks often require knowledge of the currently selected rows:
%// Tree set up
mtree = uitree(..., 'SelectionChangeFcn',@mySelectFcn);
set(mtree, 'SelectionChangeFcn',@mySelectFcn); % an alternative
%// The tree-node selection callback
function nodes = mySelectFcn(tree, value)
selectedNodes = tree.getSelectedNodes;
%// Use this to see which information is available about the node:
%// methods(selectedNodes(1),'-full')
%// And the node array:
%// methods(selectedNodes,'-full')
if ~isempty(selectedNodes) || max(selectedNodes.size)>1
%// Obtain path from selected node; Source: link1 below
nodePath = selectedNodes(1).getPath.cell;
subPathStrs = cellfun(@(p) [p.getName.char,filesep],nodePath,'un',0);
pathStr = strrep([subPathStrs{:}], [filesep,filesep],filesep);
%// Also, don't forget a drive letter here ^ if required
if ~isdir(pathStr) %// check that the selection isn't a directory
%// this is where you need to call your refresh function
end
end
end %// mySelectFcn
您可以在this answer中获得一些其他想法,其中显示了如何实现鼠标跟踪回调,以防您希望在鼠标悬停时执行刷新…