forked from claudioangione/PGA_and_C-EDGE
-
Notifications
You must be signed in to change notification settings - Fork 0
/
nhist.m
2149 lines (1966 loc) · 81.8 KB
/
nhist.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
%% description
% function [theText, rawN, x] = nhist(cellValues, 'parameter', value, ...)
%
% NHIST(x); works just like hist(x) but the resulting plot looks nice.
%
% t = NHIST(Y) bins the elements of Y into equally spaced containers
% and returns a string with information about the distributions.
% If Y is a cell array or a structure nhist will make graph the
% binned (discrete) probability density function of each data
% set for comparison on the same graph. It will return A cell
% array or structure which includes a string for each set of
% data.
%
% [t, N, X]= NHIST(...) also returns the number of items in each bin, N,
% and the locations of the left edges of each bin. If Y is a
% cell array or structure then the output is in the same form.
%
% NHIST(Y,'Property', . . . )
% NHIST(Y,'PropertyName',PropertyValue, . . . )
% See below for the different parameters.
%__________________________________________________________________________
% Summary of what function does:
% 1) Automatically sets the number and range of the bins to be appropriate
% for the data.
% 2) Compares multiple sets of data elegantly on one or more plots, with
% legend or titles. It also graphs the mean and standard deviations.
% It can also plot the median and mode.
% 3) Outputs text with the usefull statistics for each distribution.
% 4) Allows for changing many more parameters
%
% Highlighted features (see below for details)
% 'separate' to plot each set on its own axis, but with the same bounds
% 'binfactor' change the number of bins used, larger value =more bins
% 'samebins' force all bins to be the same for all plots
% 'legend' add a legend in the graph (default for structs)
% 'noerror' remove the mean and std plot from the graph
% 'median' add the median of the data to the graph
% 'text' return many details about each graph even if not plotted
% The function is robust to NaN and +-inf data points (with warnings)
%
%% Optional Properties
% Note: Alternative names to call the properties are listed at the end of each
% entry.
%__________________________________________________________________________
% Histogram and bin settings
% 'binfactor': Effects the number of bins used. A larger number
% will mean more bins used. All bins will be some
% multiple of the largest bin.
% 'binfactor','binfactors','factor','f'
% 'samebins': this will make all the bins align with each other
% the binwidth will be the mean of all the
% recomended bin sizes.
% 'minbins': The minimum number of bins allowed for each graph
% default = 10. 'minimumbins'
% 'maxbins': The maximum number of bins allowed for each graph
% default = 100. 'maximumbins'
% 'stdtimes': Number of times the standard deviation to set the
% horizontal limits of the axis, default is 4.
% 'minx': crop the axis and histogram on the left. 'xmin'
% 'maxx': crop the axis and histogram on the right. 'xmax'
% 'proportion': Plot proportion of total points on the y axis
% rather than the totaly number of points or the
% probability distribution. Useful for data sets
% with small sample sizes. 'p'
% 'pdf': Plot the pdf on the y axis
% 'numbers': Plot the raw numbers on the graph. 'number'
% 'smooth': Plot a smooth line instead of the step function.
% 'int': Force it to make bins along integers. If you like
% pass 1 or 0 to force int bins, or relax the
% restriction if it is imposed automatically.
% 'integer','discrete','intbins'
%__________________________________________________________________________
% Text related parameters
% 'titles','legend': A cell array with strings to put in the legend or
% titles. Also used for text output. 'title'
% 'nolengend': In case you pass a struct, you may force a legend
% to disappear. You will have no way to track the
% data.
% 'text': Outputs all numbers to text, even ones that are
% not plotted, this will include the number of
% points, mean, standard deviation, standard error,
% median, and approximate mode., 't','alltext'
% 'decimalplaces': Number of decimal places numbers will be output
% with, 'decimal', 'precision', 'textprecision'
% 'npoints': this will add (number of points) to the legend or
% title automatically for each plot. 'points'
% 'xlabel': Label of the lowest X axis
% 'ylabel': Label of the Y axis, note that the ylabel default
% will depend on the type of plot used, it will vary
% from 'pdf' (or probability distribution) for
% regular plots, 'number' for separate plots (the
% number of elements) and 'proportion' for
% proportion plots. Setting this parameter will
% override the defaults.
% 'fsize': Font size, default 12. 'fontsize'
% 'location': Sets the location of the legend,
% example:NorthOutside. 'legendlocation'
%__________________________________________________________________________
% Peripheral elements settings
% 'box': This will put a nice boxplot above your histogram.
% It is a typical box and whiskers plot with a red
% line for median, '+' for the mean, a blue box
% around the 25% and 75% quartiles and whiskers
% bounding 9% and 91%. When comparing multiple plots
% the boxplots are colored to match the histogram.
% 'boxplot','bplot'
% 'median': This will plot a stem plot of the median
% 'mode': This will plot a stem plot of the mode
% If both 'mode' and 'median' are passed, the mode
% will be plotted with a dashed line.
% 'serror': Will put the mean and 'standard error' bars above
% the plot rather than the default standard
% deviation. 'serrors','stderror','stderrors','sem'
%
% 'noerror': Will remove the mean and standard deviation error
% bars from above the plot. 'noerrors,
% 'linewidth': Sets the width of the lines for all the graphs
% 'color': Sets the colors of the lines.
% 'qualitative' forces each line to be most
% distinguishable, up to 12 different colors.
% 'sequential' forces the colors into a smooth
% spectrum from red to blue.
% 'colormap' will take the colors from the existing
% colormap, allowing you to choose them freely.
% 'jet' setting the parameter to an regular colormap
% will choose the colors from that map
% 'jet','gray','summer','cool', etc.
% For 'separate' plots color will specify the color of the
% bar graphs. You must use the [R G B] standard
% color definitions.
%__________________________________________________________________________
% General Figure Settings
% 'separate': Plot each histogram separately, also use normal
% bar plots for the histograms rather than the
% stairs function. Data will not be normalized.
% 'separateplots','plotseparately','normalhist','normal','s'
% 'newfig': Will make a new figure to plot it in. When using
% 'separateplots' 'newfig' will automatically
% change the size of the figure.
% 'eps': EPS file name of the generated plot to save. It
% will automatically print if you pass this
% parameter
%
%% The bin width is defined in the following way
% Disclaimer: this function is specialized to compare data with comparable
% standard deviations and means, but greatly varying numbers of points.
%
% Scotts Choice used for this function is a theoretically ideal way of
% choosing the number of bins. Of course the theory is general and so not
% rigorous, but I feel it does a good job.
% (bin width) = 3.5*std(data points)/(number of points)^(1/3);
%
% I did not follow it exactly though, restricting smaller bin sizes to be
% divisible by the larger bin sizes. In this way the different conditions
% can be accurately compared to each other.
%
% The bin width is further adulterated by user parameter 'binFactor'
% (new bin width) = (old bin width) / (binFactor);
% it allows the user to make the bins larger or smaller to their tastes.
% Larger binFactor means more bins. 1 is the default
%
%Source: http://en.wikipedia.org/wiki/Histogram#Number_of_bins_and_width
%
%% Default function behaviour
%
% If you pass it a structure, the field names will become the legend. All
% of the data outputted will be in structure form with the same field
% names. If you pass a cell array, then the output will be in cell form. If
% you pass an array or vector then the data is outputted as a string and
% two arrays.
%
% standard deviation will be plotted as a default, unless one puts in the
% 'serror' paramter which will plot the standard error = std/sqrt(N)
%
% There is no maximum or minimum X values.
% minBins=10; The minimum number of bins for the histogram
% maxBins=100;The maximum number of bins for a histogram
% AxisFontSize = 12; 'fsize' the fontsize of everything.
% The number of data points is not displayed
% The lines in the histograms are black
% faceColor = [.7 .7 .7]; The face of the histogram is gray.
% It will plot inside a figure, unless 'newfig' is passed then it will make
% a new figure. It will take over and refit all axes.
% linewidth=2; The width of the lines in the errobars and the histogram
% stdTimes=4; The axes will be cutoff at a maximum of 4 times the standard
% deviation from the mean.
% Different data sets will be plotted with a different number of bins.
%% Acknowledgments
% Thank you to the AP-Lab at Boston University for funding me while I
% developed this function. Thank you to the AP-Lab, Avi and Eli for help
% with designing and testing it and the Mathworks community for comments!
%% Examples
% Cell array example:
% A={randn(1,10^5),randn(10^3,1)+1};
% nhist(A,'legend',{'\mu=0','\mu=1'});
% nhist(A,'legend',{'\mu=0','\mu=1'},'separate');
%
% A=[randn(1,10^5)+1 randn(1,2*10^5)+5];
% nhist(A,'mode')
%
% Structure example:
% A.mu_is_Zero=randn(1,10^5); A.mu_is_Two=randn(10^3,1)+2;
% nhist(A);
% nhist(A,'color','summer')
% nhist(A,'color',[.3 .8 .3],'separate')
% nhist(A,'binfactor',4)
% nhist(A,'samebins')
% nhist(A,'median','noerror')
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Jonathan Lansey 2010-2013, %
% questions to Lansey at gmail.com %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [theText,rawN, x] = nhist(cellValues, varargin)
%% INITIALIZE PARAMETERS
% Default initialization of the parameters,
stdTimes=4; % the number of times the standard deviation to set the upper end of the axis to be.
binFactor=0.5;
sameBinsFlag=0; % if 1 then all bins will be the same size
proportionFlag=0;
pdfFlag = 0;
numberFlag = 0;
smoothFlag = 0;
intbinsForcedFlag = 0;
intbinsFlag = 0;
% These are used later to set the output parameters right.
structFlag=0;
arrayFlag=0;
minX=[]; % for the axis in case users don't enter anything
maxX=[];
minBins=10;
maxBins=150;
SXLabel = '';
yLabelFlag=0;
EPSFileName = '';
Title = '';
AxisFontSize = 12;
npointsFlag=0;
legendLocation='best';
forceNoLegend=0;
% lineColor = [.49 .49 .49];
lineColor = [0 0 0];
faceColor = [.7 .7 .7];
vertLinesForcedFlag=0;
multicolorFlag=0;
brightnessExponent=1/2;
plotStdFlag = 1; % 1 if either serror or std will be plotted
serrorFlag = 0;
medianFlag = 0;
modeFlag = 0;
boxplotFlag = 0;
textFlag=0;
decimalPlaces=2;
legendExists=0;
linewidth=2;
newfigFlag=0;
barFactor=0.5;
normalHist=0;
logFlag = 0;
logFunc = @(x) x;
%% Interpret the user parameters
k = 1;
while k <= length(varargin)
if ischar(varargin{k})
switch (lower(varargin{k}))
case {'legend','titles','title'}
cellLegend=varargin{k+1};
legendExists=1;
k = k + 1;
case {'location','legendlocation'}
legendLocation=varargin{k+1};
k = k + 1;
case 'nolegend'
forceNoLegend=1;
case 'xlabel'
SXLabel = varargin{k + 1};
k = k + 1;
case 'ylabel'
SYLabel = varargin{k + 1};
yLabelFlag=1;
k = k + 1;
case {'minx','xmin'}
minX = varargin{k + 1};
k = k + 1;
case {'maxx','xmax'}
maxX = varargin{k + 1};
k = k + 1;
case {'minbins','minimumbins'}
minBins = varargin{k + 1};
k = k + 1;
case {'maxbins','maximumbins'}
maxBins = varargin{k + 1};
k = k + 1;
case 'stdtimes' % the number of times the standard deviation to set the upper end of the axis to be.
stdTimes = varargin{k + 1};
k = k + 1;
if ischar(stdTimes)
fprintf(['\nstdTimes set to: ' stdTimes]);
error('stdTimes must be a number')
end
case {'binfactor','binfactors','factor','f'}
binFactor = varargin{k + 1};
k = k + 1;
if ischar(binFactor)
error('binFactor must be a number')
end
case {'samebins','samebin','same'}
sameBinsFlag=1;
case {'proportion','p','fraction','frac','percent','normal'}
proportionFlag=1;
case 'pdf'
pdfFlag=1;
case {'numbers','number'}
numberFlag = 1;
case {'smooth','smoooth','filter','filt'}
smoothFlag = 1;
case {'log'}
logFlag = 1;
logFunc = @(x) 10.^x;
case {'int','integer','discrete','intbins','intbin'}
intbinsForcedFlag = 1;
intbinsFlag=1;
if k+1<= length(varargin)
temp = varargin{k + 1};
if ~ischar(temp) % if its a number then we want to use it.
intbinsFlag=temp;
k=k+1;
end
end
case 'eps'
EPSFileName = varargin{k + 1};
k = k + 1;
case {'fsize','fontsize'}
AxisFontSize = varargin{k + 1};
k = k + 1;
case 'linewidth'
linewidth = varargin{k + 1};
k = k + 1;
case {'color','colors'}
lineColor=varargin{k+1};
if ischar(lineColor)
% if strcmp(lineColor,'multicolor')
multicolorFlag = 1;
% end
else %then lineColor will be redone later
faceColor = lineColor;
end
k = k + 1;
case {'npoints','points'}
npointsFlag=1;
case {'lines','line'}
vertLinesFlag=1;
vertLinesForcedFlag=vertLinesForcedFlag+1;
case {'noline','nolines'}
vertLinesFlag=0;
vertLinesForcedFlag=vertLinesForcedFlag+1;
case { 'decimalplaces','decimal','precision','textprecision'}
decimalPlaces=varargin{k+1};
k=k+1;
case {'newfig','newfigure'}
newfigFlag=true;
case {'noerror','noerrors'}
plotStdFlag = 0;
case {'serror','serrors','stderror','stderrors','sem'}
serrorFlag = 1;
case {'boxplot','bplot','box'} % surprise! undocumented feature.
boxplotFlag = 1;
plotStdFlag = 0;
case {'barwidth','barfactor','errorbarwidth'}
barFactor = varargin{k+1};
k = k+1;
case {'median','medians'}
medianFlag=1;
case {'separateplots','separate','plotseparately','normalhist','s'}
normalHist=1;
case {'mode','modes'}
modeFlag = 1;
case {'text','alltext','t'}
textFlag=1;
otherwise
warning('user entered parameter is not recognized')
disp('unrecognized term is:'); disp(varargin{k});
end
end
k = k + 1;
end
%%
% intbinsForcedFlag
% intbinsFlag
%% Check if data is an array, not a cell
valueInfo=whos('cellValues');
valueType=valueInfo.class;
switch valueType
case 'cell' % There are a few cells there, it will run as usual.
% normalHist=what you set it to, or zero;
case 'struct'
structFlag=1;
tempValues=cellValues; clear('cellValues');
if legendExists
warning(['The legend you entered will be ignored and replaced '...
'with field names. Use a cell array to pass your own legend, '...
'or rename the fields']);
else % set it to Exists,
if forceNoLegend
legendExists=0;
else
legendExists=1;
end
end
forStructLegend=fields(tempValues)';
for k=1:length(forStructLegend)
cellValues{k}=tempValues.(forStructLegend{k});
end
cellLegend =forStructLegend; % this is for text purposes.
otherwise % Its an array and data needs to be changed to a cell array, for what follows.
arrayFlag=1;
cellValues={cellValues};
normalHist=1;
if legendExists % it is probably passed as a string not a cell
valueInfo=whos('cellLegend');
valueType=valueInfo.class;
if ~strcmp(valueType,'cell')
cellLegend={cellLegend};
else
warning('please pass the legend as the same type as the data');
end
end
end
%% Check some user-entered parameters for problems
if round(decimalPlaces)~=decimalPlaces
warning(['decimalPlaces must be an integer number. You entered ' num2str(decimalPlaces) ' '...
'the rounded number ' num2str(round(decimalPlaces)) ' will be used instead']);
decimalPlaces=round(decimalPlaces);
end
if vertLinesForcedFlag>1
warning(['you cannot specify having and not having vertical lines. ' ...
'Therefore we will determine it automatically as usual']);
vertLinesForcedFlag=0;
end
%% Collect the Data, check some things
num2Plot=length(cellValues);
if legendExists
if num2Plot~=length(cellLegend)
warning('legend is not appropriately sized for the data');
if num2Plot>length(cellLegend)
for k=length(cellLegend)+1:num2Plot
cellLegend{k}=['Plot #' num2str(k)];
end
end
end
end
% check for negative and zero values if logFlag is used.
if logFlag
for k=1:num2Plot
badVals = sum(cellValues{k}<=0);
if badVals>0
warning([num2str(badVals) ' were <=0 and had to be removed from the analysis, try adding +1']);
end
end
end
% check for integer, or near integer values (to make integer bins without gaps)
if intbinsFlag
intbins=ones(1,num2Plot);
else
intbins=zeros(1,num2Plot);
if ~intbinsForcedFlag % if its forced, then we want it to stay zero
for k=1:num2Plot
intbins(k) = isdiscrete(cellValues{k});
end
if sameBinsFlag % then if one has integers, they all must be plotted along integers.
intbins = or(intbins,sum(intbins));
end
end
end
% if logflag then take that log here!
if logFlag
for k=1:num2Plot
cellValues{k} = log10(cellValues{k});
end
end
% This is to collect the means std, and a few other things
for k=1:num2Plot
if ~isnumeric(cellValues{k})
error(['You cannot make a histogram of non-numeric data. Plot #' num2str(k) ' non numeric']);
end
% infFlag(k)=;
% Changing numbers to doubles makes the 'text' parameter work.
% This also makes it so you can pass it a full matrix, just for fun.
% It is done for each one individually so you can even pass a cell array
% where the individual vectors are differently angled.
cellValues{k}=double(cellValues{k}(:));
nanValues = isnan(cellValues{k});
nnan = sum(nanValues);
% remove NaN values
if nnan>0
cellValues{k}=cellValues{k}(~nanValues);
if nnan>1, waswere='were'; else waswere='was';end
warning(['data set #:' num2str(k) ' has ' num2str(nnan) ' ''NaN'' values which ' waswere ' removed from all analysis and counts\n']);
end
% Check for and deal with infinite values.
infValues=isinf(cellValues{k});
if sum(infValues)%infFlag(k)
plotStdFlag=0; % the mean of the data makes no sense here.
if sum(infValues)>1, waswere='were'; else waswere='was';end
warning(['data set #:' num2str(k) ' has ''inf'' values which ' waswere ' put in the end bin/s. The mean and std will not be displayed\n']);
infV=cellValues{k}(infValues);
nPosInf=sum(infV>0);
nNegInf=sum(infV<0);
cellValues{k}=cellValues{k}(~infValues);
else
nPosInf=0;
nNegInf=0;
end
% Store the values temporarily
Values=cellValues{k};
% check for imaginary data
if ~isreal(Values)
warning('magnitude taken of all imaginary data');
Values=abs(Values);
cellValues{k}=Values;
end
% check if it is an empty bin
if isempty(Values)
isData(k)=false;
Values=[0 0 0];
cellValues{k}=Values;
warning(['data set #:' num2str(k) ' is empty']);
else
isData(k)=true;
end
% Store a few other useful values
stdV{k}=std(Values); % standard dev of values
meanV{k}=mean(Values);
medianV{k}=median(Values);
numPoints{k}=length(Values); % number of values
% initialize to be used later, not a user paramter at all
modeShift{k}=0;
end
if sum(isData)<1
warning('None of your data is plottable, so nothing was plotted. Please search for more data and try again');
return;
end
%% FIND THE AXIS BOUNDS
% on each side, left and right
% error the user if they choose retarded bounds (pardon my politically uncorrectness)
if ~isempty(minX) && ~isempty(maxX)
if maxX<minX
error(['your max bound: ' num2str(maxX) ' is bigger than your min bound: ' num2str(minX) ' you can''t do that silly']);
end
end
for k=1:num2Plot
Values=cellValues{k};
% warn error if there is only one point of data
if length(Values)<2
warning(['maybe a histogram is not the best method to graph your single number in plot#:' num2str(k)]);
end
% warn the user if they chose a dumb bounds (but not retarded ones)
if ~isempty(minX)
if minX>meanV{k}
warning(['the mean of your data set#' num2str(k) ' is off the chart to the left, '...
'choose larger bounds or quit messing with the program and let it do '...
'its job the way it was designed.']);
end
end
if ~isempty(maxX)
if maxX<meanV{k}
warning(['the mean of your data set#' num2str(k) ' is off the chart to the right, '...
'choose larger bounds or quit messing with the program and let it do '...
'its job the way it was designed.']);
end
end
% Note the check stdV{k}>0, just in case the std is zero, we need to set
% boundaries so that it doesn't crash with 0 bins used. The range of
% (+1,-1) is totally arbitrary.
% set x MIN values
if isempty(minX) % user did not specify - then we need to find the minimum x value to use
if stdV{k}>(10*eps) % just checking there are more than two different points to the data, checking for rounding errors.
leftEdge = meanV{k}-stdV{k}*stdTimes;
if leftEdge<min(Values) % if the std is larger than the largest value
minS(k)=min(Values);
else % cropp it now on the bottom.
% cropped!
minS(k) = leftEdge;
end
else % stdV==0, wow, all your data points are equal
minS(k)=min(Values)-1000*eps; % padd it by 100, seems reasonable
end
else % minX is specified so minS is just set stupidly here
if minX<max(Values)
minS(k)=minX;
else % ooh man, even your biggest value is smaller than your min
minS(k)=min(Values);
warning(['user parameter minx=' num2str(minX) ' override since it put all your data out of bounds']);
end
end
% set x MAX values
if isempty(maxX)
if stdV{k}>(10*eps) % just checking there are more than two different points to the data
rightEdge = meanV{k}+stdV{k}*stdTimes;
if rightEdge>max(Values) % if the suggested border is larger than the largest value
maxS(k)=max(Values);
else % crop the graph to cutoff values
maxS(k)=rightEdge;
end
else % stdV==0, wow,
% Note that minX no longer works in this case.
maxS(k)=max(Values)+1000*eps; % padd it by 100, seems reasonable
end
else % maxX is specified so minS is just set here
maxS(k)=maxX;
if maxX>min(Values)
maxS(k)=maxX;
else % ooh man, even your smallest value is bigger than your max
maxS(k)=max(Values);
warning(['user parameter maxx=' num2str(maxX) ' override since it put all your data out of bounds']);
end
end
if intbins(k)
maxS(k)=round(maxS(k))+.5;
minS(k)=round(minS(k))-.5; % subtract 1/2 to make the bin peaks appear on the numbers.
end
end % look over k finished
% This is the range that the x axis will plot at for each one.
% Only set the bounds for things that have data
% isData = logical(isData);
SXRange = [min(minS(isData)) max(maxS(isData))];
% note that later there will be a bit added to maxS of SXRange
% This below is to get estimates for appropriate binsizes
totalRange=diff(SXRange); % if the range is zero, then make it eps instead.
%% deal with the infinity data
% In this case we add the inf values back in as off the charts numbers on
% the appropriate side. In this way the 'cropped' star will be plotted.
for k=1:num2Plot % nNegInf= number of negative infinities removed
cellValues{k}=[cellValues{k}; repmat(SXRange(1)-100,nNegInf,1); repmat(SXRange(2)+100,nPosInf,1)];
end
%% FIND OUT IF THERE WERE CROPS DONE
for k=1:num2Plot
% Set the crop flag
if min(cellValues{k})<SXRange(1)
cropped_left{k}=sum(cellValues{k}<SXRange(1)); % flag to plot the star later
else
cropped_left{k}=0;
end
% Set the crop flag
if max(cellValues{k})>SXRange(2)
cropped_right{k}=sum(cellValues{k}>SXRange(2)); % flag to plot the star later
else
cropped_right{k}=0;
end
end
%% DEAL WITH BIN SIZES
% Reccomend a bin width
binWidth=zeros(1,num2Plot);
% Warn users for dumb max/min bin size choices.
if minBins<3, error('No I refuse to plot this you abuser of functions, the minimum number of bins must be at least 3'); end;
if minBins<10, warning('you are using a very small minimum number of bins, do you even know what a histogram is?'); end;
if minBins>20, warning('you are using a very large minimum number of bins, are you sure you *always need this much precision?'); end;
if maxBins>200,warning('you are using a very high maximum for the number of bins, unless your monitor is in times square you probably won''t need that many bins'); end;
if maxBins<50, warning('you are using a low maximum for the number of bins, are you sure it makes sense to do this?'); end;
% Choose estimate bin widths
for k=1:num2Plot
% This formula "Scott's choice" is described in the introduction above.
% default: binFactor=1;
binWidth(k)=3.5*stdV{k}/(binFactor*(numPoints{k})^(1/3));
% Instate a mininum and maximum number of bins
numBins = totalRange/binWidth(k); % Approx number of bins
if numBins<minBins % if this will imply less than 10 bins
binWidth(k)=totalRange/(minBins); % set so there are ten bins
end
if numBins>maxBins % if there would be more than 75 bins (way too many)
binWidth(k)=totalRange/maxBins;
end
% Check if it is intbins, becase then:
if intbins(k)% binwidth must be an integer, and it must be at least 1
binWidth(k)=max(round(binWidth(k)),1);
end
if numBins>=30 && proportionFlag
warning('it might not make sense to use ''proportion'' here since you have so many bins')
end
if numBins>=100 && (proportionFlag || numberFlag)
warning('it might make sense to use ''pdf'' here since you have so many bins')
end
nBins(k)=totalRange/binWidth(k);
% if there is enough space to plot them, then plot vertical lines.
% 30 bins is arbitrarily chosen to be the number after which there are
% vertical lines plotted by default
if nBins(k)<30
vertLinesArray(k)=1;
else
vertLinesArray(k)=0;
end
end
% fix the automatic decision if vertical lines were specified by the user
if vertLinesForcedFlag
vertLinesArray=vertLinesArray*0+vertLinesFlag;
end
% only plot lines if they all can be plotted.
% also creates one flag, so the 'array' does not need to be used.
vertLinesFlag=prod(vertLinesArray); % since they are zeros and 1's, this is an "and" statement
% find the maximum bin width
bigBinWidth=max(binWidth);
%% resize bins to be multiples of each other - or equal
% sameBinsFlag will make all bin sizes the same.
% Note that in all these conditions the largest histogram bin width
% divides evenly into the smaller bins. This way the data will line up and
% you can easily visually compare the values in different bins
if sameBinsFlag % if 'same' is passed then make them all equal to the average reccomended size
binWidth=0*binWidth+mean(binWidth); %
else % the bins will be different if neccesary
for k=1:num2Plot
% The 'ceil' rather than 'round' is supposed to make sure that the
% ratio is at lease 1 (divisor at least 2).
binWidth(k)=bigBinWidth/ceil(bigBinWidth/binWidth(k));
end
end
SXRange(2) = SXRange(2)+max(binWidth);
% recalculate totalRange for the axis lims, and histogram calculating.
totalRange=diff(SXRange);
%% CALCULATE THE HISTOGRAM
%
maxN=0; %for setting they ylim(maxN) command later, find the largest height of a column
for k=1:num2Plot
Values=cellValues{k};
% Set the bins
% Note that the range is already expanded by one half, to center columns on numbers
if intbins(k)
SBins{k}=SXRange(1):binWidth(k):SXRange(2);
% if it is 'samebins' then even if 'intbins' is zero for some of
% them to start with, it is already enforced that they *all have
% intbins if at least one of them has it, and there are 'samebins'
else
SBins{k}=SXRange(1):binWidth(k):SXRange(2);
end
% Set it to count all those outside as well.
binsForHist{k}=SBins{k};
binsForHist{k}(1)=-inf; binsForHist{k}(end)=inf;
% Calculate the histograms
n{k} = histc(Values, binsForHist{k});
if ~isData(k) % so that the ylim property is not destroyed with maxN being extra large
if normalHist
n{k}=n{k}*0+1; % it will plot it from 0 to one,
else % it needs to be the lowest minimum possible!
n{k}=n{k}*0+eps;
end
else
n{k}=n{k}';
end
% This here is to complete the right-most value of the histogram.
% x{k}=[SBins{k} SXRange(2)+binWidth(k)];
x{k} = SBins{k};
% n{k}=[n{k} 0];
% Later we will n`eed to plot a line to complete the left start.
%% Add the number of points used to the legend
if legendExists
oldLegend{k}=cellLegend{k};
else
oldLegend{k}=['Plot #' num2str(k)];
end
if npointsFlag
if legendExists
cellLegend{k}=[cellLegend{k} ' (' num2str(numPoints{k}) ')'];
else
cellLegend{k}=['(' num2str(numPoints{k}) ')'];
if k==num2Plot % only once they have all been made
legendExists=1;
end
end
end
end
%% Extra calculations for histogram
rawN=n; % save the rawN before normalization
for k=1:num2Plot
% Normalize, normalize all the data by area
% only do this if they will be plotted together, otherwise leave it be.
if (~normalHist && ~proportionFlag && ~numberFlag) || pdfFlag
% n = (each value)/(width of a bin * total number of points)
% n = n /(Total area under histogram);
n{k}=n{k}/(binWidth(k)*numPoints{k});
end % if it is a normalHist - then it will be numbers automatically.
if proportionFlag
% n = n /(Total number of points);
% now if you sum all the heights (not the areas) you get one,
n{k}=n{k}/numPoints{k};
end
% Find the maximum for plotting the errorBars
maxN=max([n{k}(:); maxN]);
% this calculates the approximate mode, the highest peak here.
% you need to add the binWidth/2 to get to the center of the bar for
% the histogram.
roundedMode{k}=mean(x{k}(n{k}==max(n{k})))+binWidth(k)/2;
end
%% CREATE THE FIGURE
if newfigFlag % determine figure height
scrsz = get(0,'ScreenSize');
sizes=[650 850 1000 scrsz(4)-8];
if num2Plot>=5
figHeight=sizes(4);
elseif num2Plot>2
figHeight=sizes(num2Plot-2);
end
if normalHist && num2Plot>2
% figure('Name', Title,'Position',[4 300 335 figHeight% ]);
figure('Position',[4 4 435 figHeight ]);
else % no reason to stretch it out so much, use default size
figure;
end
% figure('Name', Title);
Hx = axes('Box', 'off', 'FontSize', AxisFontSize);
title(makeTitle(Title));
else % all we need to do is make sure that the old figures holdstate is okay.
%save the initial hold state of the figure.
hold_state = ishold;
if ~hold_state
if normalHist && num2Plot>1
% you need to clear the whole figure to use sub-plot
clf;
else
cla; %just in case we have some subploting going on you don't want to ruin that
axis normal;
legend('off'); % in case there was a legend up.
% is there anything else we need to turn off? do it here.
end
end
end
hold on;
%% PREPARE THE COLORS
if normalHist %
if multicolorFlag
% lineStyleOrder=linspecer(num2Plot,'jet');
faceStyleOrder=linspecer(num2Plot,lineColor);
for k=1:num2Plot % make the face colors brighter a bit
lineStyleOrder{k}=[0 0 0];
faceStyleOrder{k}=(faceStyleOrder{k}).^(brightnessExponent); % this will make it brighter than the line
end
else % then we need to make all the graphs the same color, gray or not
for k=1:num2Plot
lineStyleOrder{k}=[0 0 0];
faceStyleOrder{k}=faceColor;
end
end
else % they will all be in one plot, its simple. there is no faceStyleOrder
if ischar(lineColor) % then the user must have inputted it!
% That means we should use the colormap they gave
lineStyleOrder=linspecer(num2Plot,lineColor);
else % just use the default 'jet' colormap.
lineStyleOrder=linspecer(num2Plot,'qualitative');
end
end
%% PLOT THE HISTOGRAM
% reason for this loop:
% Each histogram plot has 2 parts drawn, the legend will look at these
% colors, this just seems like an easy way to make sure that is all plotted
% in the right order - not the most effient but its fast enough as it is.
% it is plotted below the x axis so it will never appear
if normalHist % There will be no legend for the normalHist, therefore this loop is not needed.
% But we might as well run it to set the fontsize here:
for k=1:num2Plot
if num2Plot>1
subplot(num2Plot,1,k);
end
hold on;
plot([.5 1.5],[-1 -1],'color',lineStyleOrder{k},'linewidth',linewidth);
set(gca,'fontsize',AxisFontSize);
end
else % do the same thing, but on different subplots
for k=1:num2Plot
% Do not put: if isData(k) here because it is important that even
% places with no data have a reserved color spot on a legend.
% plot lines below the x axis, they will never show up but will set the
% legend appropriately.
plot([.5 1.5],[-1 -1],'color',lineStyleOrder{k},'linewidth',linewidth);
end
set(gca,'fontsize',AxisFontSize);
end
if normalHist % plot on separate sub-plots
for k=1:num2Plot
if num2Plot>1
subplot(num2Plot,1,k);
end
hold on;
if isData(k)
% Note this is basically doing what the 'histc' version of bar does,
% but with more functionality (there must be some matlab bug which
% doesn't allow changing the lineColor property of a histc bar graph.)
if vertLinesFlag % then plot the bars with edges
bar(logFunc(x{k}+binWidth(k)/2),n{k}/1,'FaceColor',faceStyleOrder{k},'barwidth',1,'EdgeColor','k','linewidth',1.5)
else % plot the bars without edges
bar(logFunc(x{k}+binWidth(k)/2),n{k}/1,'FaceColor',faceStyleOrder{k},'barwidth',1,'EdgeColor','none')
end
if ~smoothFlag
stairs(logFunc(x{k}),n{k},'k','linewidth',linewidth);
plot(logFunc([x{k}(1) x{k}(1)]),[0 n{k}(1)],'color','k','linewidth',linewidth);
else % plot it smooth, skip the very edges.
% plot(x{k}(1:end-1)+binWidth(k)/2,n{k}(1:end-1),'k','linewidth',linewidth);
% xi = linspace(SXRange(1),SXRange(2),500); yi = pchip(x{k}(1:end-1)+binWidth(k)/2,n{k}(1:end-1),xi);
xi = linspace(SXRange(1)-binWidth(k)/2,SXRange(2)+binWidth(k)/2,500);
% not to (end-1) like above, so we got an extra digit
yi = pchip([x{k}(1)-binWidth(k)/2, x{k}(1:end)+binWidth(k)/2],[0 n{k}(1:end-1) 0],xi);
plot(logFunc(xi),yi,'k','linewidth',linewidth);
end
end
end
else % plot them all on one graph with the stairs function
for k=1:num2Plot
if isData(k)
if ~smoothFlag
stairs(logFunc(x{k}),n{k},'color',lineStyleOrder{k},'linewidth',linewidth);
plot(logFunc([x{k}(1) x{k}(1)]),[0 n{k}(1)],'color',lineStyleOrder{k},'linewidth',linewidth);
else % plot it smooth