Home > classes > @ao > hist.m

hist

PURPOSE ^

HIST overloads the histogram function (hist) of MATLAB for Analysis Objects.

SYNOPSIS ^

function varargout = hist(varargin)

DESCRIPTION ^

 HIST overloads the histogram function (hist) of MATLAB for Analysis Objects.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

 DESCRIPTION: HIST overloads the histogram function (hist) of MATLAB for
              Analysis Objects.

 CALL:        b = hist(a)
              b = hist(a, N)
              b = hist(a, X)
              b = hist(a, pl)

 INPUTS:      a  - input analysis object(s)
              N  - number of bins
              X  - specify centers of bins
              pl - a parameter list

 OUTPUTS:     b  - xydata type analysis object(s) containing the
                   histogrammed data

 PARAMETERS:  'N'    - number of bins
              'X'    - set of bin centers

 VERSION:     $Id: hist.m,v 1.9 2008/01/20 12:53:29 hewitson Exp $

 The following call returns a parameter list object that contains the
 default parameter values:

 >> pl = hist(ao, 'Params')

 HISTORY: 24-05-07 M Hewitson
             Creation

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SUBFUNCTIONS ^

SOURCE CODE ^

0001 function varargout = hist(varargin)
0002 % HIST overloads the histogram function (hist) of MATLAB for Analysis Objects.
0003 %
0004 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0005 %
0006 % DESCRIPTION: HIST overloads the histogram function (hist) of MATLAB for
0007 %              Analysis Objects.
0008 %
0009 % CALL:        b = hist(a)
0010 %              b = hist(a, N)
0011 %              b = hist(a, X)
0012 %              b = hist(a, pl)
0013 %
0014 % INPUTS:      a  - input analysis object(s)
0015 %              N  - number of bins
0016 %              X  - specify centers of bins
0017 %              pl - a parameter list
0018 %
0019 % OUTPUTS:     b  - xydata type analysis object(s) containing the
0020 %                   histogrammed data
0021 %
0022 % PARAMETERS:  'N'    - number of bins
0023 %              'X'    - set of bin centers
0024 %
0025 % VERSION:     $Id: hist.m,v 1.9 2008/01/20 12:53:29 hewitson Exp $
0026 %
0027 % The following call returns a parameter list object that contains the
0028 % default parameter values:
0029 %
0030 % >> pl = hist(ao, 'Params')
0031 %
0032 % HISTORY: 24-05-07 M Hewitson
0033 %             Creation
0034 %
0035 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0036 
0037 ALGONAME = mfilename;
0038 VERSION  = '$Id: hist.m,v 1.9 2008/01/20 12:53:29 hewitson Exp $';
0039 
0040 %% Check if this is a call for parameters
0041 if nargin == 2
0042   if isa(varargin{1}, 'ao') && ischar(varargin{2})
0043     in = char(varargin{2});
0044     if strcmp(in, 'Params')
0045       varargout{1} = getDefaultPL();
0046       return
0047     elseif strcmp(in, 'Version')
0048       varargout{1} = VERSION;
0049       return
0050     end
0051   end
0052 end
0053 
0054 %---------------- capture input variable names, AOs, and plists
0055 Nbins  = -1;
0056 Xbins  = [];
0057 
0058 % Collect input ao's, plist's and ao variable names
0059 in_names = {};
0060 for ii = 1:nargin
0061   in_names{end+1} = inputname(ii);
0062 
0063   % Collect Xbins and Nbins
0064   if isnumeric(varargin{ii})
0065     if length(varargin{ii}) > 1
0066       Xbins = varargin{ii};
0067     else
0068       Nbins = varargin{ii};
0069     end
0070   end
0071 end
0072 
0073 [as, ps, invars] = collect_inputs(varargin, in_names);
0074 
0075 % produce one plist
0076 if isa(ps, 'plist')
0077   pl = combine(ps, getDefaultPL());
0078 else
0079   pl = getDefaultPL();
0080 end
0081 
0082 % If we have X from command input, override plist
0083 if ~isempty(Xbins)
0084   pl = pset(pl, 'X', Xbins);
0085 end
0086 
0087 % If we have N from command input, override plist
0088 if Nbins>0
0089   pl = pset(pl, 'N', Nbins);
0090 end
0091 
0092 % Get parameters
0093 N = find(pl, 'N');
0094 X = find(pl, 'X');
0095 
0096 %---------------- Loop over input AOs
0097 
0098 % Initialise output
0099 bs = [];
0100 
0101 % start looping
0102 for j=1:numel(as)
0103   a = as(j);
0104 
0105   % get data
0106   [x, y] = get_xy_values(a.data);
0107 
0108   % Histogram this data
0109   if isempty(X)
0110     %disp(sprintf('** sorting data into %d bins', N));
0111     [n,x] = hist(y, N);
0112   else
0113     %disp(sprintf('** sorting data into %d bins', length(X)));
0114     [n,x] = hist(y, X);
0115   end
0116 
0117   %-------- Build output AOs
0118 
0119   % make a new xydata object
0120   xy = xydata(x, n);
0121   xy = set(xy, 'name', sprintf('hist(%s)', a.data.name));
0122   xy = set(xy, 'xunits', a.data.yunits);
0123   xy = set(xy, 'yunits', 'Count');
0124 
0125   % make a new history object
0126   h = history(ALGONAME, VERSION, pl, a.hist);
0127   h = set(h, 'invars', invars(j));
0128 
0129   % make output analysis object
0130   b = ao(xy, h);
0131 
0132   % name for this object
0133   b  = setnh(b, 'name', sprintf('hist(%s)', invars{j}));
0134   bs = [bs b];
0135 
0136 end % end of AO loop
0137 
0138 % Reshape the ouput to the same size of the input
0139 bs = reshape(bs, size(as));
0140 
0141 
0142 %---------------- Set outputs
0143 varargout{1} = bs;
0144 
0145 %----------------
0146 %----------------
0147 %----------------
0148 %----------------
0149 
0150 %--------------------------------------------------------------------------
0151 
0152 %% Get default params
0153 function plo = getDefaultPL()
0154 
0155 plo = plist();
0156 plo = append(plo, param('N', 10));
0157 plo = append(plo, param('X', []));
0158 
0159 % END

Generated on Tue 22-Jan-2008 10:39:13 by m2html © 2003