Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Show listed images in Panel when a node is clicked in UITree Matlab?

I am working in Matlab. I want to show Images in a given Panel one by one when user mouse click on a file name in tree list. Can anyone please help me?

enter image description here

Code for tree:

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 to Display Image in panel:

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

I just need to know how to call function refreshDisplay() from my tree. Again remember I want to call function from Tree Elements(files) not from node(sub directory).

Regards.

like image 252
Gujjar Avatar asked Mar 18 '23 15:03

Gujjar


2 Answers

Below is a quick but complete example. Code is commented and easy to follow:

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

like image 156
Amro Avatar answered Apr 06 '23 10:04

Amro


Note that everything in the tree are nodes, both the folders and the images. You need to implement a check inside the selection callback to test whether the selected node is a folder.

Quoting UndocumentedMatlab:

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

link1


You can get some other ideas in this answer, which shows how to implement a mouse tracking callback, in case you want the refresh to execute on a mouse-over...

like image 41
Dev-iL Avatar answered Apr 06 '23 11:04

Dev-iL