No BSD License  

image thumbnail

ibar.m

by

 

24 Jun 2004 (Updated )

ibar() does the same as bar(), but returns an individual handle for each bar.

[h]=ibar(varargin)
function [h]=ibar(varargin)
%
%    ibar does the same as bar, but returns a handle for each individual bar:
%    if the input is a vector, ibar returns a vector of handles
%    if the input contains a matrix, ibar returns a cell array of handles
%    
%    ibar(X,Y) draws the columns of the M-by-N matrix Y as M groups of N
%    vertical bars.  The vector X must be monotonically increasing or
%    decreasing.
%    ibar(Y) uses the default value of X=1:M.  For vector inputs, ibar(X,Y)
%    or ibar(Y) draws LENGTH(Y) bars.  

%    6-24-2004    
%    Robert Schleicher
%    r.schleicher@uni-koeln.de

v=version;
if (str2num(v(1))>=7) %Version check: MATLAB bar function changed in Version 7
    oldh=bar('v6',varargin{:}); %use the version 6 type
else
    oldh=bar(varargin{:});
end;
hold on; %hold actual plot
vtemp=get(oldh,'Vertices'); %keep Vertices
xlims=get(gca,'XLim'); %keep axes limits
ylims=get(gca,'YLim');
delete(oldh); %then delete old patch;
h=[];
if iscell(vtemp);
    for j=1:size(vtemp,1)
        vtemp2=vtemp{j};
        color=0.1*j+0.1; %each group of bars gets a different color
        h2=[];
        for i=2:5:length(vtemp2-3)     %now re-draw, but bar by bar                
            htemp=patch(vtemp2(i:(i+3),1),vtemp2(i:(i+3),2),color); %draw single bar
            h2=[h2;htemp]; %keep its handle                     
        end
    h{j}=h2;
    end
else    
    for i=2:5:length(vtemp-3)     %now re-draw, but bar by bar
        htemp=patch(vtemp(i:(i+3),1),vtemp(i:(i+3),2),1); %draw single bar
        h=[h;htemp]; %keep its handle     
    end
    
end;
set(gca,'XLim',xlims)
set(gca,'YLim',ylims)
hold off

Contact us