在UITree Matlab中单击节点时,在Panel中显示列出的图像?

时间:2014-08-28 12:06:06

标签: image matlab user-interface treeview jtree

我在Matlab工作。当用户鼠标单击树列表中的文件名时,我想逐个显示给定面板中的图像。有人可以帮帮我吗?

enter image description here

树的代码:

src = strcat(pwd,'\Result\');
[mtree, container] = uitree('v0', 'Root',src,۔۔۔
'Position',[10 10 290 370],'Parent', imageListPanel2); % Parent is ignored
 set(container, 'Parent', imageListPanel2);  % fix the uitree Parent

在面板中显示图像的功能:

function refreshDisplay(varargin)
    imgDisplayed = imshow(tmp,'parent',workingAx);
end %refreshDisplay

我只需要知道如何从树中调用函数refreshDisplay()。再次记住,我想从树元素(文件)调用函数而不是从节点(子目录)调用函数。

问候。

2 个答案:

答案 0 :(得分:5)

以下是一个快速但完整的示例。代码已注释且易于理解:

function MyImageViewer
    % prepare GUI
    hFig = figure('Menubar','none', 'Name','Image Viewer');
    hPan(1) = uipanel('Parent',hFig, 'Position',[0 0 0.3 1]);
    hPan(2) = uipanel('Parent',hFig, 'Position',[0.3 0 0.7 1]);

    ax = axes('Parent',hPan(2), 'Units','normalized', 'Position',[0 0 1 1]);
    axis(ax, 'off', 'image')

    [jtree,htree] = uitree('v0', 'Parent',hFig, ...
        'Root','C:\Users\Amro\Pictures\', 'SelectionChangeFcn',@changeFcn);
    set(htree, 'Parent',hPan(1), 'Units','normalized', 'Position',[0 0 1 1]);
    jtree.expand(jtree.getRoot);    % expand root node

    % list of supported image extensions
    fmt = imformats;
    imgExt = lower([fmt.ext]);

    function changeFcn(~,~)
        % get selected node
        nodes = jtree.getSelectedNodes;
        if isempty(nodes), return; end
        n = nodes(1);

        % only consider a leaf node (skip folders)
        if ~n.isLeaf, return; end

        % get complete node path (absolute filename)
        p = arrayfun(@(nd) char(nd.getName), n.getPath, 'Uniform',false);
        p = strjoin(p(:).', filesep);

        % check for supported image types, and show image
        [~,~,ext] = fileparts(p);
        if any(strcmpi(ext(2:end),imgExt))
            imshow(p, 'Parent',ax)
        end
    end
end

image_viewer

答案 1 :(得分:2)

请注意,树中的所有内容都是节点,包括文件夹和图像。您需要在选择回调中执行检查,以测试所选节点是否为文件夹。

引用UndocumentedMatlab

  

可以使用mtree.getSelectedNodes访问当前选定的节点。节点选择   回调通常需要了解当前选定的行:

%// 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

link1


您可以在this answer中获得一些其他想法,其中显示了如何实现鼠标跟踪回调,以防您希望在鼠标悬停时执行刷新...