time | calls | line |
---|
| | 1 | function [no,xo] = hist(varargin)
|
| | 2 | %HIST Histogram.
|
| | 3 | % N = HIST(Y) bins the elements of Y into 10 equally spaced containers
|
| | 4 | % and returns the number of elements in each container. If Y is a
|
| | 5 | % matrix, HIST works down the columns.
|
| | 6 | %
|
| | 7 | % N = HIST(Y,M), where M is a scalar, uses M bins.
|
| | 8 | %
|
| | 9 | % N = HIST(Y,X), where X is a vector, returns the distribution of Y
|
| | 10 | % among bins with centers specified by X. The first bin includes
|
| | 11 | % data between -inf and the first center and the last bin
|
| | 12 | % includes data between the last bin and inf. Note: Use HISTC if
|
| | 13 | % it is more natural to specify bin edges instead.
|
| | 14 | %
|
| | 15 | % [N,X] = HIST(...) also returns the position of the bin centers in X.
|
| | 16 | %
|
| | 17 | % HIST(...) without output arguments produces a histogram bar plot of
|
| | 18 | % the results. The bar edges on the first and last bins may extend to
|
| | 19 | % cover the min and max of the data unless a matrix of data is supplied.
|
| | 20 | %
|
| | 21 | % HIST(AX,...) plots into AX instead of GCA.
|
| | 22 | %
|
| | 23 | % Class support for inputs Y, X:
|
| | 24 | % float: double, single
|
| | 25 | %
|
| | 26 | % See also HISTOGRAM, HISTCOUNTS, HISTC, MODE.
|
| | 27 |
|
| | 28 | % Copyright 1984-2014 The MathWorks, Inc.
|
| | 29 |
|
| | 30 | % Parse possible Axes input
|
| 52 | 31 | narginchk(1,inf);
|
0.02 | 52 | 32 | [cax,args,nargs] = axescheck(varargin{:});
|
| 52 | 33 | y = args{1};
|
| 52 | 34 | if nargs == 1
|
| | 35 | x = 10;
|
| 52 | 36 | else
|
| 52 | 37 | x = args{2};
|
| 52 | 38 | end
|
| 52 | 39 | if isvector(y)
|
| 52 | 40 | y = y(:);
|
| 52 | 41 | end
|
| | 42 |
|
| | 43 |
|
| | 44 | % for backward compatibility, logical is allowed in hist.m
|
| 52 | 45 | if (~isnumeric(x) && ~islogical(x)) || (~isnumeric(y) && ~islogical(y))
|
| | 46 | error(message('MATLAB:hist:InvalidInput'))
|
| | 47 | end
|
| | 48 |
|
| | 49 | % Cache the vector used to specify how bins are created
|
| 52 | 50 | N = x;
|
| | 51 |
|
| 52 | 52 | if isempty(y)
|
| | 53 | if isscalar(x)
|
| | 54 | x = 1:double(x);
|
| | 55 | end
|
| | 56 | nn = zeros(size(x)); % No elements to count
|
| | 57 | % Set miny, maxy for call to bar below.
|
| | 58 | miny = [];
|
| | 59 | maxy = [];
|
| | 60 | edges = [-Inf Inf];
|
| 52 | 61 | else
|
| 52 | 62 | if isreal(y)
|
| 52 | 63 | miny = min(y(:));
|
| 52 | 64 | maxy = max(y(:));
|
| 52 | 65 | if ~isfinite(miny) || ~isfinite(maxy)
|
| | 66 | % Ignore Infs and NaNs when computing miny and maxy.
|
| | 67 | yind = y(isfinite(y));
|
| | 68 | if isempty(yind)
|
| | 69 | % All entries in y are not finite.
|
| | 70 | miny = zeros(class(y));
|
| | 71 | maxy = zeros(class(y));
|
| | 72 | else
|
| | 73 | miny = min(yind);
|
| | 74 | maxy = max(yind);
|
| | 75 | end
|
| | 76 | end
|
| | 77 | else
|
| | 78 | % Ignore Infs and NaNs when computing miny and maxy.
|
| | 79 | yind = y(isfinite(y));
|
| | 80 | miny = min(yind);
|
| | 81 | maxy = max(yind);
|
| | 82 | if isempty(miny)
|
| | 83 | % All entries in y are not finite.
|
| | 84 | miny = zeros(class(y));
|
| | 85 | maxy = zeros(class(y));
|
| | 86 | end
|
| | 87 | end
|
| | 88 |
|
| 52 | 89 | if isscalar(x)
|
| 52 | 90 | if miny == maxy
|
| | 91 | miny = miny - floor(x/2) - 0.5;
|
| | 92 | maxy = maxy + ceil(x/2) - 0.5;
|
| | 93 | end
|
0.01 | 52 | 94 | edges = linspace(miny,maxy,x+1);
|
| 52 | 95 | if x == 0
|
| | 96 | binwidth = Inf;
|
| 52 | 97 | else
|
| 52 | 98 | binwidth = edges(2) - edges(1);
|
| 52 | 99 | end
|
| 52 | 100 | x = edges(1:end-1) + binwidth/2; % form x
|
| | 101 | % Set edges for call to bar below.
|
| 52 | 102 | edges(1) = -Inf;
|
| 52 | 103 | edges(end) = Inf;
|
| | 104 | else
|
| | 105 | edges = x(:)';
|
| | 106 | binwidth = diff(edges);
|
| | 107 | % Set edges for call to bar below.
|
| | 108 | edges = [-Inf, edges(1:end-1)+binwidth/2, Inf];
|
| | 109 | end
|
| | 110 |
|
| | 111 | % For compatibility
|
| 52 | 112 | edges = full(real(edges));
|
| 52 | 113 | y = full(real(y));
|
| | 114 |
|
| | 115 | % Shift bins so the interval is ( ] instead of [ ) for
|
| 52 | 116 | edgesc = edges + eps(edges);
|
| 52 | 117 | edgesc(1) = -Inf;
|
| 52 | 118 | edgesc(end) = Inf;
|
| 52 | 119 | nn = histc(y,edgesc,1);
|
| | 120 |
|
| | 121 | % Combine last bin with next to last bin
|
| 52 | 122 | if size(nn,1) > 1
|
0.01 | 52 | 123 | nn(end-1,:) = nn(end-1,:)+nn(end,:);
|
| 52 | 124 | end
|
| 52 | 125 | nn = nn(1:end-1,:);
|
| | 126 |
|
| 52 | 127 | end
|
| | 128 |
|
| 52 | 129 | if nargout > 0
|
| 52 | 130 | if isvector(y) && ~isempty(y) % Return row vectors if possible.
|
| 52 | 131 | no = nn';
|
| 52 | 132 | xo = x;
|
| | 133 | else
|
| | 134 | no = nn;
|
| | 135 | xo = x';
|
| | 136 | end
|
| | 137 | else
|
| | 138 | if ~isempty(cax)
|
| | 139 | histPatch = bar(cax,x,nn,[miny maxy],'hist');
|
| | 140 | if strcmpi(get(ancestor(cax,'figure'),'Visible'),'off')
|
| | 141 | return;
|
| | 142 | end
|
| | 143 | else
|
| | 144 | histPatch = bar(x,nn,[miny maxy],'hist');
|
| | 145 | end
|
| | 146 |
|
| | 147 | % Add linked plot and brushing behavior objects to the patch
|
| | 148 | varName = inputname(1+~isempty(cax));
|
| | 149 | for k=1:length(histPatch)
|
| | 150 | linkBehavior = hggetbehavior(histPatch(k),'Linked');
|
| | 151 | linkBehavior.DataSourceFcn = {@localDataChange linkBehavior N};
|
| | 152 | if ~isempty(varName)
|
| | 153 | if ~isvector(y)
|
| | 154 | linkBehavior.DataSource = getcolumn(varName,k,'expression');
|
| | 155 | else
|
| | 156 | linkBehavior.DataSource = varName;
|
| | 157 | end
|
| | 158 | end
|
| | 159 | linkBehavior.BrushFcn = {@localBrushFunc linkBehavior x};
|
| | 160 | linkBehavior.LinkBrushFcn = {@localSetLinkedBrushing};
|
| | 161 | brushBehavior = hggetbehavior(histPatch(k),'Brush');
|
| | 162 | brushBehavior.DrawFcn = {@localDrawFunc brushBehavior histPatch(k)};
|
| | 163 | if isempty(get(histPatch(k),'DisplayName'))
|
| | 164 | set(histPatch(k),'DisplayName',linkBehavior.DataSource)
|
| | 165 | end
|
| | 166 | datacursorBehavior = hggetbehavior(histPatch(k),'datacursor');
|
| | 167 | set(datacursorBehavior,'UpdateDataCursorFcn',...
|
| | 168 | {@localHistUpdateDataCursor,histPatch(k)});
|
| | 169 | set(datacursorBehavior,'MoveDataCursorFcn',...
|
| | 170 | {@localHistMoveDataCursor,histPatch(k)});
|
| | 171 | set(datacursorBehavior,'UpdateFcn',...
|
| | 172 | {@localHistDatatipCallback,x,nn(:,k),edges});
|
| | 173 | end
|
| | 174 |
|
| | 175 | % If applying to a linked plot the linked plot graphics cache must
|
| | 176 | % be updated manually since there are not yet eventmanager listeners
|
| | 177 | % to do this automatically.
|
| | 178 | f = handle(ancestor(histPatch(1),'figure'));
|
| | 179 | if ~isempty(f.findprop('linkPlot')) && f.linkPlot
|
| | 180 | datamanager.updateLinkedGraphics(f);
|
| | 181 | end
|
| | 182 | end
|
Other subfunctions in this file are not included in this listing.