Code covered by the BSD License  

Highlights from
select current object

image thumbnail

select current object

by

 

09 Nov 2012 (Updated )

select current object by buttons. Each button represents one object which is either axes, image, etc

scov=sco(hgcf)
function scov=sco(hgcf)
%% sco: select handle of current object
%% Syntax:
%   handle_selected_object = sco
%   handle_selected_object = sco(handle_figure)
%
%% Description:
% handle_selected_object = sco(handle_figure) returns the handle of the
% current object for the figure specified by handle_figure
%
% handle_selected_object = sco assumes handle_figure = gcf


% Version: 1.0, 2012/11/9
% Version: 1.1, 2013/2/20 % fix units problem.
% Author:  Chao-Wei Chen


if nargin<1,hgcf=gcf;end
current_units=get(hgcf,'units');
if ~strcmpi(current_units,'pixels'),set(hgcf,'units','pixels');end % set units pixels temporarily

if ~isexistobj(hgcf),disp('No object to be selected');return;end
scov=[];
ha=findall(hgcf,'type','axes');
Na=numel(ha); % number of axes

hsco=figure_hsco;
for n=Na:-1:1
    this_axes=ha(n);    
    hp= uipanel('Title',hstr(this_axes),'Position',get(this_axes,'position'),'parent',hsco);    
    ham=get(this_axes,'children'); % handles of member of current axes
    
    M=numel(ham);
    hco_button=uicontrol('style','pushbutton','units','normalized','callback',@(obj,event)select_ca(obj,event,this_axes),...
            'position',[0 M/(M+1) 1 1/(M+1)],'parent',hp,'string',hstr(this_axes));
    decoration(hco_button,this_axes);  
    for m=M:-1:1 % m=1 is hp(n) , m=2~M+1 are children under hp(n)
        hco=ham(m);
        hco_button=uicontrol('style','pushbutton','units','normalized','callback',@(obj,event)select_ca(obj,event,hco),...
            'position',[0 (m-1)/(M+1) 1 1/(M+1)],'parent',hp,'string',hstr(hco));
        decoration(hco_button,hco);       
    end
end

set(gcf,'name','select current object sco')
uiwait(hsco);
if ~strcmpi(current_units,'pixels'),set(hgcf,'units',current_units);end % set units back
%%
    function hs=hstr(h)
        hs=[get(h,'type') ':' num2str(h)];
    end
    function select_ca(obj,event,scov_in)       
       scov=scov_in;
       disp([hstr(scov) ' is selected']);
       close(hsco); 
    end
    function decoration(hco_button,hco)
        switch(get(hco,'type'))
            case 'line'
                set(hco_button,'foregroundcolor',get(hco,'color'));
                set(hco_button,'string',[hstr(hco) ':' get(hco,'LineStyle') ' ' get(hco,'Marker')]);
            case 'image'                
            case 'rectangle'
                set(hco_button,'backgroundcolor',get(hco,'facecolor'));
                set(hco_button,'foregroundcolor',get(hco,'edgecolor'));
                set(hco_button,'string',[hstr(hco) ':' get(hco,'LineStyle')]);
            case 'text'
                set(hco_button,'string',[hstr(hco) ':' get(hco,'string')]);
                set(hco_button,'foregroundcolor',get(hco,'color'));
            case 'axes'
                set(hco_button,'string',hstr(hco));
        end
    end
end

function tf=isexistobj(hgcf)
tf=false;
hsp=findall(hgcf,'type','axes');
Nsp=numel(hsp); % number of subplot. Graphic object must be under axes

for n=Nsp:-1:1
    this_hsp=hsp(n);
    hspc=get(this_hsp,'children'); % handle of children of current subplot    
    if numel(hspc)>0,tf=true;return;end
end

end

function hsco=figure_hsco
hsco=findobj('tag','hsco');
if isempty(hsco),
    hsco=figure;
    set(hsco,'tag','hsco');
else
    set(hsco);
end
end

Contact us