-
Notifications
You must be signed in to change notification settings - Fork 2
/
tlabel.m
1419 lines (1338 loc) · 47.1 KB
/
tlabel.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
function tlabel(varargin)
%TLABEL Full date formatted tick labels with ZOOM, PAN and LINKAXES.
%
% SYNTAX:
% tlabel
% tlabel(...,TICKAXIS)
% tlabel(...,DATEFORM)
% tlabel(...,'keeplimits') vs 'freelimits'
% tlabel(...,'keepticks') vs 'freeticks'
% tlabel(...,PRO,VAL,...)
% tlabel(AX,...)
% tlabel(...);
%
% INPUT: (all optionals)
% TICKAXIS - Date axis. One of 'x', 'y' or 'z'.
% DEFAULT: 'x'
% DATEFORM - Numerical or string date format to be used on the
% ticks. See DATESTR for details.
% DEFAULT: (as DATETICK, see NOTE below)
% 'keeplimits' - Preserves the axis limits or 'freelimits' to not.
% DEFAULT: (does it if axis limit mode is 'manual')
% 'keepticks' - Preserves the ticks locations or 'freeticks' to not.
% DEFAULT: (does it if axis tick mode is 'manual')
% PRO,VAL - Property/value extra inputs.
% DEFAULT: (see TABLE below for details)
% AX - Uses the specified axis, rather than the current axis.
% Axes are automatically linked if more than one were
% specified. May include figure handles see NOTE below
% for details.
% DEFAULT: gca
%
% DESCRIPTION:
% This program works as DATETICK but with ZOOM, PAN and LINK
% functionalities.
%
% It's a mayor modification of DATETICKZOOM function by Lauwerys, by
% including the LINK functionality and includes in the date axis Label
% the part of the date that was left out by the TickLabels, allowing
% the user to know the full date no matter the zooming in.
%
% Besides, it includes some extra functionalities that the user may use
% as usual Property/Value pair inputs, which are:
%
% PROPERTY POSSIBLE VALUES DESCRIPTION
% ---------------------------------------------------------------------
% 'Language' 'local' or 'en_us' Sets months language.
% DEFAULT: 'local'
%
% 'LinkOthers' false, true, or Links other axes besides
% the specified ones time?
% ('yz' for example)
% DEFAULT: false
%
% 'WhichAxes' 'all', 'none', 'first', Specifies on which axes
% 'last', or the to display the dates.
% specified axes handles
% DEFAULT: 'all'
%
% 'FixLow' An integer or zero. Indicates if more ticks
% DEFAULT: 4 should be included when
% when less than 4 are
% displayed.
%
% 'FixHigh' An integer or zero. Indicates if less ticks
% DEFAULT: 11 should be used when more
% than 11 are displayed.
%
% 'NumFmtNN' User custom (see NOTE Change the MATLAB
% below for details) default numeric date
% DEFAULT: by DATETICK format: 'NN' by the
% specified one.
%
% 'Reference' 'middle', 'first', Specifies the date on
% 'last' or 'none' the time axis to be used
% DEFAULT: 'middle' as reference for the
% whole date.
%
% 'LabelY' DEFAULT: 'yyyy' Numeric or string format
% on axis label when only
% year is displayed.
%
% 'LabelYM' DEFAULT: 'mmm yyyy' Numeric or string format
% on axis label when year
% and month are displayed.
%
% 'LabelYMD' DEFAULT: 'dd/mmm/yyyy' Numeric or string format
% on axis label when only
% year is displayed.
% ---------------------------------------------------------------------
%
% NOTE:
% * Optional inputs use its DEFAULT value when not given or [].
% * For example, by default pairwise options: ('NumFmt6','dd/mmm') and
% ('NumFmt17',12) are used. See DATETICK for details on the numeric
% formats.
% * Except from the optional AX input which must be the first one,
% all other inputs arguments may be entered in any other position
% (keeping the pairs always toghether, of course).
% * The string inputs may be as short as posible, except for its value.
% Besides, upper cases are ignored (try 'keepl' instead of
% 'KeepLimits').
% * If DATEFORMAT is used, it will be used even after any ZOOM.
% * 'keepticks' and 'keeplimits' are used by default if the TICKAXIS
% ticks and limits mode is 'manual'. So, limits are preserved if
% before TLABEL, axis tight was used.
% * Use 'Reference','none' to avoid the using of the label axis.
% * ADDITIONAL NOTES are included inside this file.
%
% EXAMPLE:
% tini = datenum([2009 05 29 12 00 00]);
% dt = 20/60/24;
% N = 100;
% t = (0:N-1)'*dt + tini;
% figure(1), clf
% subplot(311)
% plot(t,3*cos(2*pi*t/12*24))
% subplot(312)
% plot(t,5*cos(2*pi*t/6*24))
% subplot(313)
% plot(t,8*cos(2*pi*t/3*24))
% axis(findobj(gcf,'Type','axes'),'tight')
% tlabel(gcf,'keepl','W','last') % Dates printed only on last plot!
% zoom on
%
% SEE ALSO:
% DATETICK, DATESTR, DATENUM
% and
% DATETICKZOOM by Christophe Lauwerys and LINKZOOM by Carlos Vargas
% at http://www.mathworks.com/matlabcentral/fileexchange
%
%
% ---
% MFILE: tlabel.m
% VERSION: 2.6.1 (Sep 30, 2009) (<a href="matlab:web('http://www.mathworks.com/matlabcentral/fileexchange/authors/11258')">download</a>)
% MATLAB: 7.7.0.471 (R2008b)
% AUTHOR: Carlos Adrian Vargas Aguilera (MEXICO)
% CONTACT: [email protected]
% ADDITIONAL NOTES:
% * The extra option 'WhichAxes' is helpful when the user wants to link
% all the subplots in a figure, but with the date displayed only on
% the lower ones, to avoid repetition.
% * The program creates an application data (see SETAPPDATA) on each
% axes called 'tlabel' (besides of 'axesLimAndTickLink',
% 'axesTickLabelLink' and 'labelStringLink' when linking axes) with
% some stuff used by the program after zooming or panning. For
% example, to retrieve the date format used on the current axes after
% TLABEL was used, do the following:
% >> data = getappdata(gca,'tlabel');
% >> data.TicksFmt
% REVISIONS:
% 1.0 Released. (Apr 24, 2008)
% 2.0 Rewritten code. Mayor changes with inputs and link axes. No
% more outputs allowed. (Jun 08, 2009)
% 2.1 When 'keepticks' are used, now they are revised by the
% fixTicks subfunction. If the user changes manually the axes
% limits or ticks of the first axes handle, the 'keepticks' and
% 'keeplimits' are changed accordingly. (Jun 30, 2009).
% 2.1.1 Fixed small BUG with this new 'keep's options thanks to Ayal
% Anis. (Jul 03, 2009)
% 2.1.2 Fixed another smaller BUG with this new 'keep's options. (Jul
% 14, 2009)
% 2.2 Finally fixed BUG with 'keep's options. Fixed bugs with
% temporal figure creation. New 'none' option for 'Reference'.
% Fixed BUG with Double-Click. Changed application data name
% from 'tlabeldata' to 'tlabel'. (Jul 29, 2009)
% 2.3 Fixed BUG when using PLOTYY. Fixed BUG with 'WhichAxes' and
% 'Reference' parse inputs, besides this latter option now moves
% the label to the selected reference (as observed and suggested
% by Giles Lesser). (Aug 20, 2009)
% 2.4 Fixed BUG when displaying years. Fixed BUG with numeric format
% (thanks to Roger Parkyn). Added 'freeticks' and 'freelimits'
% options. (Aug 21, 2009)
% 2.5 Fixed BUG with year ticks and not empty label. Fixed BUG with
% a unique DATETICK tick (thanks to Kelly Kearney). (Aug 21,
% 2009)
% 2.6 Fixed bugs with a unique tick (thanks to Mary-Louise
% Timmermans). (Sep 07, 2009)
% 2.6.1 Fixed small bug with 'middle' reference (thanks to Ayal Anis).
% (Sep 30, 2009)
% DISCLAIMER:
% tlabel.m is provided "as is" without warranty of any kind, under the
% revised BSD license.
% Copyright (c) 2008,2009 Carlos Adrian Vargas Aguilera
% -------------------------------------------------------------------------
% MAIN
% -------------------------------------------------------------------------
% Parameters.
myAppName = 'tlabel';
zoomAppName = 'zoom_zoomOrigAxesLimits';
secPause = 0.25; % Pauses for this seconds to check double-click
if ~((nargin==2) && isstruct(varargin{2}))
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% TLABEL called from command window or a M-file
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Sets defaults.
data.AX = gca; % Gets axis handle.
data.TICKAXIS = 'x'; % Sets date axis.
data.DATEFORM = []; % Fixed date format.
data.FixLow = 4; % Adds ticks if there are < it.
data.FixHigh = 11; % Delets ticks if there are > it.
data.Which = true; % Writes Labels on date axis?
data.KeepTicks = false; % Keeps original date axis ticks?
data.KeepLimits = false; % Keeps original ticks?
data.LabelFmt = []; % Sets date label format.
data.LabelY = 'yyyy'; % Sets date format for years.
data.LabelYM = 'mmm yyyy'; % ... for years and months.
data.LabelYMD = 'dd/mmm/yyyy'; % ... for years, months and days.
data.Language = 'local'; % Sets laguage for string months.
data.LinkOthers = false; % Links other axes besides date?
data.NumFmt = ...
mat2cell([1:31 0]',ones(1,32),1); % Change the 32 MATLAB date formats?
data.NumFmt{6} = 'dd/mmm'; % Note: {32} is format 0.
data.NumFmt{17} = 12;
data.Reference = 'middle'; % Sets date label reference.
data.TicksFmt = []; % Sets date ticks format.
data.WhichAxes = 'all'; % Sets where to write the ticks.
% Gets inputs and/or defaults.
data = parseInputs(data,varargin{:});
% Checks the axes to be printed.
data.Which = getWhichAxes(data.Which,data.WhichAxes,data.AX);
% Writes labels and saves application data.
writeLabels(data.AX(1),data,false,myAppName,zoomAppName)
% Links the axes and labels and resets the ZOOM.
linkAxes(data)
% Sets the ZOOM and PAN functionality.
for k = 1:length(data.AX)
xa = data.AX(k);
zh = zoom(xa);
ph = pan(ancestor(xa,{'figure','uipanel'}));
set(zh,'ActionPostCallback',@tlabel)
set(ph,'ActionPostCallback',@tlabel)
end
else
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% TLABEL called after ZOOM or PAN
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
try
% Gets input axes handle and check it. Fixed BUG, Aug 2009
if ~isfield(varargin{2},'Axes'), return, end
AX = varargin{2}.Axes(1); % In case it is a group.
if ~ishandle(AX) || ~strcmp(get(AX,'Type'),'axes'), return, end
if ~isappdata(AX,myAppName), return, end
% Retrieves application data.
data = getappdata(AX,myAppName);
% Eliminates closed axes.
ind = false(1,length(data.AX));
for k = 1:length(data.AX)
if ~ishandle(data.AX(k))
ind(k) = true;
end
end
data.AX(ind) = [];
data.Which(ind) = [];
% Check double-click. Fixed BUG, Jul 2009
pause(secPause)
drawnow
doubleClick = strcmp('open',...
get(ancestor(AX,{'figure','uipanel'}),'SelectionType'));
% Writes labels and saves application data.
writeLabels(AX,data,doubleClick,myAppName,zoomAppName);
% Checks if double-click or zoom out on current axes.
if doubleClick || all(axis(AX)==getappdata(AX,zoomAppName))
% Zooms out all axes.
for k = 1:length(data.AX)
axis(data.AX(k),getappdata(data.AX(k),zoomAppName))
end
end
catch
% Do not sets an error during execution, only displays it as a warning.
warning('CVARGAS:tlabel:errorDuringZoomOrPanExecution',lasterr)
end
end
% =========================================================================
% SUBFUNCTIONS
% -------------------------------------------------------------------------
function writeLabels(AX,data,doubleClick,myAppName,zoomAppName)
% Writes ticks to be used.
% Get axis limits, ticks and number of ticks within.
[data,ticks,tlim] = useDatetick(AX,data,doubleClick,zoomAppName);
% Fixes ticks.
if ~isempty(data.TicksFmt) % Fixed BUG, Jun 2009
[ticks,data] = fixTicks(ticks,tlim,data);
end
% Change numerical date format?
if isempty(data.DATEFORM) && ~isempty(data.TicksFmt)
if data.TicksFmt==0, data.TicksFmt = 32; end
data.TicksFmt = data.NumFmt{data.TicksFmt};
end
% Generates the labels.
if ~isempty(data.DATEFORM)
% Fixed formatted date.
STicks = datestr(ticks,data.DATEFORM,data.Language);
[SLabel,RLabel] = dateLabel(tlim,...
data.LabelFmt,data.Reference,data.Language);
elseif ~isempty(data.TicksFmt)
% Default formmated date.
STicks = datestr(ticks,data.TicksFmt,data.Language);
[SLabel,RLabel] = dateLabel(tlim,...
data.LabelFmt,data.Reference,data.Language);
else
% No date at all.
STicks = num2str(ticks);
SLabel = '';
RLabel = [];
end
% Write new ticks and label.
for k = 1:length(data.AX)
xa = data.AX(k);
lh = get(xa,[data.TICKAXIS 'Label']);
set(xa,[data.TICKAXIS 'Lim' ],tlim);
set(xa,[data.TICKAXIS 'Tick'],ticks);
if data.Which(k)
set(xa,[data.TICKAXIS 'TickLabel'],STicks);
set(lh,'String',SLabel) % Fixed bug, Aug 2009 (thanks to Kelly Kearney)
if ~isempty(RLabel) % New feature, Aug 2009 (suggested by Giles Lesser)
temp = get(lh,'Units');
set(lh,'Units','data')
PLabel = get(lh,'Position');
PLabel(1*(strcmp(data.TICKAXIS,'x')) + ...
2*(strcmp(data.TICKAXIS,'y')) + ...
3*(strcmp(data.TICKAXIS,'z'))) = RLabel;
set(lh,'Position',PLabel)
set(lh,'Units',temp)
end
else
set(xa,[data.TICKAXIS 'TickLabel'],[]);
end
% Saves data on each axes.
setappdata(xa,myAppName,data);
end
function [data,ticks,tlim] = useDatetick(AX,data,doubleClick,zoomAppName)
% Gets ticks by using MATLAB's DATETICK.
% Gets date limits.
tlim = get(AX,[data.TICKAXIS 'Lim']);
% Generate a temporal invisible axes.
hf = ancestor(AX,{'figure','uipanel'});
tempF = get(0 ,'CurrentFigure');
tempA = get(tempF,'CurrentAxes');
tf = figure(...
'Visible' ,'off',...
'Units' ,get(hf,'Units'),...
'Position' ,get(hf,'Position'));
xa = axes(...
'Parent' ,tf,...
'Units' ,get(AX,'Units'),...
'Position' ,get(AX,'Position'),...
[data.TICKAXIS 'Lim'] ,tlim,...
[data.TICKAXIS 'Tick'],get(AX,[data.TICKAXIS 'Tick']));
set(0 ,'CurrentFigure',tempF);
set(tempF,'CurrentAxes' ,tempA);
% Optional DATETICK arguments.
opt = {};
if data.KeepLimits, opt{end+1} = 'keeplimits'; end
if data.KeepTicks, opt{end+1} = 'keepticks'; end
% Generates new TickLabels with DATETICK.
if ~isempty(data.DATEFORM)
% Manual DATEFORM.
try
datetick(xa,data.TICKAXIS,data.DATEFORM,opt{:})
catch
% Unknown DATEFORM. Uses DATESTR latter.
if isequal(data.LabelFmt,data.LabelY)
datetick(xa,data.TICKAXIS,3,opt{:}) % 'mmm'
elseif isequal(data.LabelFmt,data.LabelYM)
datetick(xa,data.TICKAXIS,7,opt{:}) % 'dd'
elseif isequal(data.LabelFmt,data.LabelYMD)
datetick(xa,data.TICKAXIS,15,opt{:}) % 'HH:MM'
elseif isnumeric(data.DATEFORM)
datetick(xa,data.TICKAXIS,10,opt{:}) % 'yyyy'
else
datetick(xa,data.TICKAXIS,opt{:})
end
end
else
% Auto DATEFORM.
datetick(xa,data.TICKAXIS,opt{:})
end
% Checks if Double-Click.
if doubleClick && isappdata(AX,zoomAppName)
axis(xa,getappdata(AX,zoomAppName))
tlim = get(xa,[data.TICKAXIS 'Lim']);
set(hf,'SelectionType','normal')
end
% Gets new date limits.
if ~data.KeepLimits
tlim = get(xa,[data.TICKAXIS 'Lim']);
data.KeepLimits = true; % All zooms preserves the new limits.
end
% Gets parameter from date axis.
ticks = get(xa,[data.TICKAXIS 'Tick']);
inticks = (ticks>=tlim(1)) & (ticks<=tlim(2));
if ~data.KeepTicks && sum(inticks(inticks))<2
% Try again. Maybe something wrong with DATEFORM.
datetick(xa,data.TICKAXIS,opt{:})
ticks = get(xa,[data.TICKAXIS 'Tick']);
inticks = (ticks>=tlim(1)) & (ticks<=tlim(2));
if sum(inticks(inticks))<2
% Try again. Maybe is the axes size. Use defaut size.
close(tf)
tf = figure(...
'Visible' ,'off');
xa = axes(...
'Parent' ,tf,...
[data.TICKAXIS 'Lim'] ,tlim,...
[data.TICKAXIS 'Tick'],get(AX,[data.TICKAXIS 'Tick']));
set(0 ,'CurrentFigure',tempF);
set(tempF,'CurrentAxes' ,tempA);
datetick(xa,data.TICKAXIS,opt{:})
ticks = get(xa,[data.TICKAXIS 'Tick']);
inticks = (ticks>=tlim(1)) & (ticks<=tlim(2));
end
end
% Checks ticks (seems to solve a problem with Double-Click).
if length(ticks)>1 && ~data.KeepTicks
inticks = find(inticks);
dticks = diff(ticks([1 2]));
if (dticks < (ticks(inticks(1))-tlim(1))) || ...
(dticks < (tlim(2)-ticks(inticks(end))))
datetick(xa,data.TICKAXIS,'keeplimits')
ticks = get(xa,[data.TICKAXIS 'Tick']);
tlim = get(xa,[data.TICKAXIS 'Lim']);
end
end
% Finds datetick and label format.
tlab = get(xa,[data.TICKAXIS 'TickLabel']);
data = findFormat(ticks(1),tlab(1,:),data);
% Clear temporal axes and figure
delete(tf)
function [labeltext,labelref] = dateLabel(tlim,LabelFmt,Reference,Language)
% Set label text.
labeltext = '';
labelref = [];
if ~isempty(LabelFmt)
switch lower(Reference(1))
case 'm'
date0 = mean(tlim);
case 'f'
date0 = tlim(1);
case 'l'
date0 = tlim(2);
case 'n'
return
end
labeltext = datestr(date0,LabelFmt,Language);
labelref = date0; % Fixed bug, Sep 2009. Thanks to Ayal Anis.
end
function Which = getWhichAxes(Which,WhichAxes,AX)
% Finds out the axes to be printed.
if ischar(WhichAxes)
% Specifyed as a char.
switch lower(WhichAxes)
case 'all'
% continue
case 'none'
Which(:) = false;
case 'first'
Which(2:end) = false;
case 'last'
Which(1:end-1) = false;
end
else
% Specifyed from axes handle.
Which(:) = false;
for k = 1:length(WhichAxes)
[a,b] = ismember(WhichAxes(k),AX);
if a
Which(b) = true;
end
end
end
function linkAxes(data)
% Links the time axes.
% Gets labels handles.
LH = get(data.AX,[data.TICKAXIS 'Label']);
if iscell(LH)
LH = cell2mat(LH);
end
% Links Limits.
axesLimAndTickLink = linkprop(data.AX,...
{[data.TICKAXIS 'Lim'],...
[data.TICKAXIS 'Tick']});
% Links labels.
if any(data.Which)
axesTickLabelLink = linkprop(data.AX(data.Which),...
[data.TICKAXIS 'TickLabel']);
labelStringLink = linkprop(LH(data.Which),'String');
else
axesTickLabelLink = [];
labelStringLink = [];
end
% Links other axes Limits?
if (islogical(data.LinkOthers) && data.LinkOthers) || ...
(isnumeric(data.LinkOthers) && data.LinkOthers)
xyz = 'xyz';
elseif ischar(data.LinkOthers)
xyz = data.LinkOthers;
else
xyz = '';
end
xyz = xyz(xyz~=data.TICKAXIS);
while ~isempty(xyz)
addprop(axesLimAndTickLink,[xyz(1) 'Lim'])
xyz(1) = [];
end
% Saves Link on axes and resets its zoom.
for k = 1:length(data.AX)
zoom( data.AX(k),'reset')
setappdata(data.AX(k),'axesLimAndTickLink',axesLimAndTickLink);
setappdata(data.AX(k),'axesTickLabelLink' ,axesTickLabelLink);
setappdata( LH(k),'labelStringLink' ,labelStringLink);
end
% Clears specified axes.
iclear = find(~data.Which);
if ~isempty(iclear)
for h = data.AX(iclear).'
set( h,[data.TICKAXIS 'TickLabelMode'],'manual')
set( h,[data.TICKAXIS 'TickLabel'],[])
set(get(h,[data.TICKAXIS 'Label']),'String','')
end
end
function data = findFormat(tick,tlab,data)
% Gets date format from DATETICK ticks and sets the format for ticks and
% label of TLABEL. If date.TicksFmt is empty, then if date.DATEFORM is
% empty then DATETICKS won't be used, else the latter DATEFORM is used, by
% Carlos Vargas.
% Get dateformats that not include year (and months (and days)).
% 0 'dd-mmm-yyyy HH:MM:SS' 15 'HH:MM'
% 1 'dd-mmm-yyyy' 16 'HH:MM PM'
% 2 'mm/dd/yy' 17 'QQ-YY'
% 3 'mmm' 18 'QQ'
% 4 'm' 19 'dd/mm'
% 5 'mm' 20 'dd/mm/yy'
% 6 'mm/dd' 21 'mmm.dd,yyyy HH:MM:SS'
% 7 'dd' 22 'mmm.dd,yyyy'
% 8 'ddd' 23 'mm/dd/yyyy'
% 9 'd' 24 'dd/mm/yyyy'
% 10 'yyyy' 25 'yy/mm/dd'
% 11 'yy' 26 'yyyy/mm/dd'
% 12 'mmmyy' 27 'QQ-YYYY'
% 13 'HH:MM:SS' 28 'mmmyyyy'
% 14 'HH:MM:SS PM' 29 'yyyy-mm-dd'
% 30 'yyyymmddTHHMMSS'
% 31 'yyyy-mm-dd HH:MM:SS'
f{1} = [3:6 18:19]; % not years
f{2} = 7:9; % not (years +) month
f{3} = 13:16; % not ((years +) month +) days
f{4} = [0:2 10:12 17 20:31]; % includes years
% Gets label length.
Nlab = length(tlab);
% Gets DATETICK format and sets label format.
for k = 1:length(f) % Fixed bug, Aug 2009
% Searches in null, -y, -y-m or -y-m-d format.
for m = f{k}
nlab = datestr(tick,m);
if (Nlab==length(nlab)) && strcmp(tlab,nlab)
% Sets the DateFormat given by datetick.
data.TicksFmt = m;
% Search and write the DateFormat for Labels (if any).
if k==1 % puts year
data.LabelFmt = data.LabelY;
elseif k==2 % puts year and month
data.LabelFmt = data.LabelYM;
elseif k==3 % puts year, month and day
data.LabelFmt = data.LabelYMD;
else
data.LabelFmt = []; % Fixed bug, Aug 2009
end
break
end
end
end
% Gets given DATEFORM and sets label format.
if isempty(data.DATEFORM)
% continue
elseif ischar(data.DATEFORM)
% Date format given as a string.
% Checks if is some default format.
switch data.DATEFORM
case 'dd-mmm-yyyy HH:MM:SS', data.DATEFORM = 0;
case 'dd-mmm-yyyy' , data.DATEFORM = 1;
case 'mm/dd/yy' , data.DATEFORM = 2;
case 'mmm' , data.DATEFORM = 3;
case 'm' , data.DATEFORM = 4;
case 'mm' , data.DATEFORM = 5;
case 'mm/dd' , data.DATEFORM = 6;
case 'dd' , data.DATEFORM = 7;
case 'ddd' , data.DATEFORM = 8;
case 'd' , data.DATEFORM = 9;
case 'yyyy' , data.DATEFORM = 10;
case 'yy' , data.DATEFORM = 11;
case 'mmmyy' , data.DATEFORM = 12;
case 'HH:MM:SS' , data.DATEFORM = 13;
case 'HH:MM:SS PM' , data.DATEFORM = 14;
case 'HH:MM' , data.DATEFORM = 15;
case 'HH:MM PM' , data.DATEFORM = 16;
case 'QQ-YY' , data.DATEFORM = 17;
case 'QQ' , data.DATEFORM = 18;
case 'dd/mm' , data.DATEFORM = 19;
case 'dd/mm/yy' , data.DATEFORM = 20;
case 'mmm.dd,yyyy HH:MM:SS', data.DATEFORM = 21;
case 'mmm.dd,yyyy' , data.DATEFORM = 22;
case 'mm/dd/yyyy' , data.DATEFORM = 23;
case 'dd/mm/yyyy' , data.DATEFORM = 24;
case 'yy/mm/dd' , data.DATEFORM = 25;
case 'yyyy/mm/dd' , data.DATEFORM = 26;
case 'QQ-YYYY' , data.DATEFORM = 27;
case 'mmmyyyy' , data.DATEFORM = 28;
case 'yyyy-mm-dd' , data.DATEFORM = 29;
case 'yyyymmddTHHMMSS' , data.DATEFORM = 30;
case 'yyyy-mm-dd HH:MM:SS' , data.DATEFORM = 31;
otherwise
%continue
end
% Now sets the label format.
if isnumeric(data.DATEFORM)
% Search and write the DateFormat for Labels (if any).
if ismember(data.DATEFORM,f{1}) % puts year
data.LabelFmt = data.LabelY;
elseif ismember(data.DATEFORM,f{2}) % puts year and month
data.LabelFmt = data.LabelYM;
elseif ismember(data.DATEFORM,f{3}) % puts year, month and day
data.LabelFmt = data.LabelYMD;
else
data.LabelFmt = []; % Just clears the label
end
else
% Searches for label format from the string DATEFORM.
if ~isempty(strfind(data.DATEFORM,'yy'))
data.LabelFmt = [];
elseif ~isempty(strfind(data.DATEFORM,'mm'))
data.LabelFmt = data.LabelY;
elseif ~isempty(strfind(data.DATEFORM,'dd'))
data.LabelFmt = data.LabelYM;
elseif ~isempty(strfind(data.DATEFORM,'HH'))
data.LabelFmt = data.LabelYMD;
elseif ~isempty(strfind(data.DATEFORM,'MM'))
data.LabelFmt = [data.LabelYMD ' HH'];
elseif ~isempty(strfind(data.DATEFORM,'SS'))
data.LabelFmt = [data.LabelYMD ' HH:MM'];
else
data.LabelFmt = []; % Just clears the label
end
end
else % isnumeric(data.DATEFORM)
% DateFormat given in numeric form.
% Search and write the DateFormat for Labels (if any).
if ismember(data.DATEFORM,f{1}) % puts year
data.LabelFmt = data.LabelY;
elseif ismember(data.DATEFORM,f{2}) % puts year and month
data.LabelFmt = data.LabelYM;
elseif ismember(data.DATEFORM,f{3}) % puts year, month and day
data.LabelFmt = data.LabelYMD;
else
data.LabelFmt = []; % Just clears the label
end
end
function [ticks,data] = fixTicks(ticks,tlim,data)
% Adds some ticks when DATETICKS puts just a few or deletes some if it
% puts too many. The quantities are specified by data.FixLow and
% data.FixHigh. Use 0 quantity if adjust is not required. By Carlos
% Vargas
% Sets minimum and maximum number of ticks.
MinDayTicks = 4;
if data.KeepTicks
MinDayTicks = length(ticks);
elseif (data.FixLow==0)
MinDayTicks = -Inf;
elseif data.FixLow>1
MinDayTicks = data.FixLow;
end
MaxDayTicks = 11;
if data.KeepTicks
MaxDayTicks = length(ticks);
elseif (data.FixHigh==0)
MaxDayTicks = Inf;
elseif data.FixHigh>1
MaxDayTicks = data.FixHigh;
end
if MaxDayTicks <= MinDayTicks
MaxDayTicks = MinDayTicks;
end
% Checks maximum number of ticks.
inticks = sum((ticks>=tlim(1)) & (ticks<=tlim(2)));
if (data.FixHigh~=0)
ticks2 = ticks;
inticks2 = inticks;
k = 0;
while (inticks2>max(MaxDayTicks,1)) && (k<3) % Fixed bug, Aug 2009
k = k+1;
ticks2 = ticks2(1:2:end);
inticks2 = sum((ticks2>=tlim(1)) & (ticks2<=tlim(2)));
end
ticks = ticks2;
inticks = inticks2;
end
% Checks minimum number of ticks and its format.
if data.FixLow~=0
kmax = 4;
k = 0;
if data.KeepTicks
tickskeep = ticks;
end
if ~isempty(data.DATEFORM)
LabelFmt = data.LabelFmt;
end
% Tries to increase the number of ticks after some trials.
while (k<kmax) && ((inticks<MinDayTicks) || data.KeepTicks)
k = k+1;
% 0 'dd-mmm-yyyy HH:MM:SS' 15 'HH:MM'
% 1 'dd-mmm-yyyy' 16 'HH:MM PM'
% 2 'mm/dd/yy' 17 'QQ-YY'
% 3 'mmm' 18 'QQ'
% 4 'm' 19 'dd/mm'
% 5 'mm' 20 'dd/mm/yy'
% 6 'mm/dd' 21 'mmm.dd,yyyy HH:MM:SS'
% 7 'dd' 22 'mmm.dd,yyyy'
% 8 'ddd' 23 'mm/dd/yyyy'
% 9 'd' 24 'dd/mm/yyyy'
% 10 'yyyy' 25 'yy/mm/dd'
% 11 'yy' 26 'yyyy/mm/dd'
% 12 'mmmyy' 27 'QQ-YYYY'
% 13 'HH:MM:SS' 28 'mmmyyyy'
% 14 'HH:MM:SS PM' 29 'yyyy-mm-dd'
% 30 'yyyymmddTHHMMSS'
% 31 'yyyy-mm-dd HH:MM:SS'
% Checks the date part icluded in the format.
isformat = [0 0 0 0 0 0];
if ismember(data.TicksFmt,[0 13:14 21 30:31]) % Seconds
isformat(6) = 1;
elseif ismember(data.TicksFmt,15:16) % Hours and minutes
isformat(4) = 1;
elseif ismember(data.TicksFmt,[1:2 6:9 19:26 29]) % Days
isformat(3) = 1;
elseif ismember(data.TicksFmt,[3:5 12 28]) % Months
isformat(2) = 1;
elseif ismember(data.TicksFmt,10:11) % Years
isformat(1) = 1;
end
% Now adds ticks corresponding to the format.
% SECONDS ===============================================================
if isformat(6)
% Checks size.
if length(ticks)<2, continue, end
% Changes serial date to vector.
ticks = datevec(ticks+0.1/86400); ticks(:,6) = round(ticks(:,6));
% Gets date interval in seconds.
dt = round((datenum(ticks(2,1:6))-datenum(ticks(1,1:6)))*86400);
% Gets increase factor from factorization.
dtf = factor(dt); dtf = dtf(1);
% Gets new date interval.
dt = dt/dtf;
% Gets new number of ticks.
nticks = (size(ticks,1)+1)*dtf;
% Generates new ticks.
yyt = repmat(ticks(1,1),nticks,1);
mmt = repmat(ticks(1,2),nticks,1);
ddt = repmat(ticks(1,3),nticks,1);
HHt = repmat(ticks(1,4),nticks,1);
MMt = repmat(ticks(1,5),nticks,1);
SSt = ((0:nticks-1)'-dtf)*dt + ticks(1,6);
ticks = datenum([yyt mmt ddt HHt MMt SSt]);
% Checks ticks outside limits.
inticks = (ticks>=tlim(1)) & (ticks<=tlim(2));
ticks(~inticks) = [];
inticks(~inticks) = [];
inticks = sum(inticks(inticks));
if inticks<2, continue, end
% Checks maximum number of ticks.
if data.FixHigh
l = 0;
while (inticks>MaxDayTicks) && (l<3)
l = l+1;
tempticks = ticks;
ticks = ticks(1:2:end);
inticks = sum((ticks>=tlim(1)) & (ticks<=tlim(2)));
if inticks<MinDayTicks
ticks = tempticks;
k = Inf;
break
end
end
end
% Checks if seconds were not used. 'HH:MM' to be used.
ticks2 = datevec(ticks(1:2)+0.1/86400);
if round(ticks2(1,6))==round(ticks2(2,6))
data.TicksFmt = 15;
end
% HOURS or MINUTES ======================================================
elseif isformat(4) || isformat(5)
% Checks size. 'HH:MM:SS' to be used?
if length(ticks)<2, k = k-1; data.TicksFmt = 13; continue, end
% Changes serial date to vector.
ticks = datevec(ticks+0.1/86400);
if ticks(2,5)~=ticks(1,5)
% MINUTES.
% Gets date interval in minutes.
dt = round((datenum([ticks(2,1:5) 0])-datenum([ticks(1,1:5) 0]))*1440);
else % ((ticks(1,4)+ticks(2,4))==0) || (ticks(2,4)~=ticks(1,4))
% HOURS
% Gets date interval in hours.
dt = round((datenum([ticks(2,1:4) 0 0]) - ...
datenum([ticks(1,1:4) 0 0]))*24);
end
% 'HH:MM:SS' to be used?
if dt==0
ticks = datenum(ticks);
k = k-1;
data.TicksFmt = 13;
continue
end
% Gets increase factor from factorization.
dtf = factor(dt); dtf = dtf(1);
% Gets new date interval.
dt = dt/dtf;
% Gets new number of ticks.
nticks = (size(ticks,1)+1)*dtf;
% Generates new ticks.
yyt = repmat(ticks(1,1),nticks,1);
mmt = repmat(ticks(1,2),nticks,1);
ddt = repmat(ticks(1,3),nticks,1);
if ticks(2,5)~=ticks(1,5)
% MINUTES.
HHt = repmat(ticks(1,4),nticks,1);
MMt = ((0:nticks-1)'-dtf)*dt + ticks(1,5);
SSt = zeros(nticks,1);
else % ((ticks(1,4)+ticks(2,4))==0) || (ticks(2,4)~=ticks(1,4))
% HOURS
HHt = ((0:nticks-1)'-dtf)*dt + ticks(1,4);
MMt = zeros(nticks,1);
SSt = MMt;
end
ticks = datenum([yyt mmt ddt HHt MMt SSt]);
% Checks ticks outside limits.
inticks = (ticks>=tlim(1)) & (ticks<=tlim(2));
% 'HH:MM:SS' to be used?
if sum(inticks(inticks))<2
k = k-1;
data.TicksFmt = 13;
inticks = sum(inticks(inticks));
continue
end
% Clears bad ticks.
ticks(~inticks) = [];
inticks = sum(inticks(inticks));
% Checks maximum number of ticks.
if data.FixHigh
l = 0;
while (inticks>MaxDayTicks) && (l<3)
l = l+1;
tempticks = ticks;
ticks = ticks(1:2:end);
inticks = sum((ticks>=tlim(1)) & (ticks<=tlim(2)));
if inticks<MinDayTicks
ticks = tempticks;
k = Inf;
break
end
end
end
% Checks if seconds are used or if minutes/hours are not used.
ticks2 = datevec(ticks(1:2)+0.1/86400);
if round(ticks2(1,6))~=round(ticks2(2,6))
% 'HH:MM:SS' to be used.
data.TicksFmt = 13;
elseif (round(ticks2(1,5))==round(ticks2(2,5))) && ...
round(ticks2(1,4))==round(ticks2(2,4))
% Minutes/hours not used. 'mm/dd' to be used.
data.TicksFmt = 6;
data.LabelFmt = data.LabelY;
elseif (k==kmax) && (inticks<MinDayTicks)
k = 0;
% 'HH:MM:SS' to be used.
data.TicksFmt = 13;
end
% DAYS ==================================================================
elseif isformat(3)
% Checks size. 'HH:MM' to be used?
if length(ticks)<2
k = k-1;
data.TicksFmt = 15;
data.LabelFmt = data.LabelYMD;
continue
end
% Changes serial date to vector.
ticks = datevec(ticks);
% Gets date interval in days.
dt = datenum([ticks(2,1:3) 0 0 0])-datenum([ticks(1,1:3) 0 0 0]);
if dt==0
ticks = datenum(ticks);
k = k-1;
% 'HH:MM' to be used.
data.TicksFmt = 15;
data.LabelFmt = data.LabelYMD;
continue
end
if dt>27 && dt<32
% One month interval, decreased to 15 days.
nticks = 2*size(ticks,1);
% Generates new ticks.
yyt = ticks(:,1);
mmt = ticks(:,2);
yyt = [yyt yyt].'; yyt = yyt(:);
mmt = [mmt mmt].'; mmt = mmt(:);
ddt = [ones(nticks/2,1) repmat(15,nticks/2,1)].'; ddt = ddt(:);
else
% Just increases the old interval
% Gets increase factor from factorization.
dtf = factor(round(dt)); dtf = dtf(1);
% Gets new date interval.
dt = dt/dtf;
% Gets new number of ticks.
nticks = size(ticks,1); nticks = dtf*(nticks+1);
% Generates new ticks.
yyt = repmat(ticks(1,1),nticks,1);
mmt = repmat(ticks(1,2),nticks,1);
ddt = ((0:nticks-1)'-dtf)*dt + ticks(1,3);
end
HHt = zeros(nticks,1);
MMt = HHt;
SSt = MMt;
ticks = datenum([yyt mmt ddt HHt MMt SSt]);
% Checks ticks outside limits.
inticks = (ticks>=tlim(1)) & (ticks<=tlim(2));
% 'HH:MM' to be used?
if sum(inticks(inticks))<2
k = k-1;
data.TicksFmt = 15;
data.LabelFmt = data.LabelYMD;
inticks = sum(inticks(inticks));
continue
end
% Clears bad ticks.
ticks(~inticks) = [];
inticks = sum(inticks(inticks));
% Checks maximum number of ticks.
if data.FixHigh
l = 0;
while (inticks>MaxDayTicks) && (l<3)