This repository was archived by the owner on Feb 6, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathXYscatter.m
More file actions
2290 lines (1944 loc) · 82.1 KB
/
Copy pathXYscatter.m
File metadata and controls
2290 lines (1944 loc) · 82.1 KB
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 [fig,ax] = XYscatter(varargin)
% XYscatter is custom plotting program based on MATLAB plot function.
%
% SYNTAX:
% XYscatter(X,Y)
% XYscatter(X1,Y1,X2,Y2,...,Xn,Yn)
% XYscatter(fX1,Y1,X2,Y2,...,Xn,Yng)
% XYscatter(...,'PropertyName', propertyvalue,...)
% [fig,ax] = XYscatter(...)
%
% DESCRIPTION:
% XYscatter(X,Y) creates a standard figure the same as the MATLAB func-
% tion plot for the numeric data provided in the X and Y variables. Ad-
% ditionally, XYscatter will prompt to input x- and y-axis labels, title,
% and legend entries.
% XYscatter(X1,Y1,X2,Y2,...,Xn,Yn) operates as above but allows the user
% to input pairs of X and Y variables as in the the MATLAB function plot.
% XYscatter(fX1,Y1,X2,Y2,...,Xn,Yng) operates as exactly as the previous
% example; however, the input is contained in a cell array.
% XYscatter(...,'PropertyName', propertyvalue,...) operates as above,
% but allows for the control of the plot using property modifers similar
% to the standard figgure and axes properties built into MATLAB.
% [fig,ax] = XYscatter(...) operates exactly asdescribed but returns the
% figure and axes handles that are generated.
%
% PROGRAM OUTLINE:
% 1 - INITILIZE PARAMETERS
% 2 - SEPERATES DATA FROM OPTIONS
% 3 - SEPERATE THE PROPERTY MODIFIERS AND SECONDARY AXIS DATA
% 4 - PREPARE DATA FOR PLOTTING
% 5 - PLOT DATA
% 6 - BUILD SLIDERS, LIMITBOXES, AND CONTEXT MENUS
% 7 - APPLY SETTINGS AND PLOT LIMITS
% 8 - STORE ABSOLULTE LIMITS OF DATA
% SUBFUNCTION: subfunction_options
% SUBFUNCTION/CALLBACK: setup
% SETUP >> SUBFUNCTION: annotatefigure
% SETUP >> SUBFUNCTION: linespec
% SETUP >> LINESPEC >> SUBFUNCTION: findvalue
% SETUP >> SUBFUNCTION: addlegend
% SETUP >> SUBFUNCTION: getaxislabels
% SETUP >> SUBFUNCTION: toggle
% SUBFUNCTION: subfunction_preparedata
% SUBFUNCTION_PREPAREDATA >> SUBFUNCTION: trimdata
% SUBFUNCTION: subfunction_plotdata
% SUBFUNCTION: subfunction_plotdata >> plotcontour
% SUBFUNCTION: subfunction_appenddata
% SUBFUNCTION: subfunction_plotci
% SUBFUNCTION: subfunction_builduicontrols
% SUBFUNCTION: subfuction_figuremenu
% SUBFUNCTION: subfunction_linemenu
% SUBFUNCTION: subfunction_textmenu
% SUBFUNCTION: slider_control
% SUBFUNCTION: limit_control
% SUBFUNCTION: subfunction_limits
% SUBFUNCTION_LIMITS >> SUBFUNCTION: set_axis
% SUBFUNCTION: subfunction_sliderlimits
% CALLBACK: callback_editline
% CALLBACK: callback_editlinecolor
% CALLBACK: callback_deleteline
% CALLBACK: callback_export
% CALLBACK: callback_limitbox
% CALLBACK: callback_slider
% CALLBACK: callback_scroll
% CALLBACK: callback_stepsize
% CALLBACK: callback_zoom
% CALLBACK: callback_movetext
% CALLBACK: callback_movecursor
% CALLBACK: callback_contoursection
%__________________________________________________________________________
% 1 - INITILIZE PARAMETERS
opt = {}; % User supplied property modifier options
% 2 - SEPERATES DATA FROM OPTIONS
% 2.1 - When input data is a cell array, all remaining inputs are
% considered property modifier inputs
if iscell(varargin{1});
C1 = varargin{1};
if nargin > 1; opt = varargin(2:nargin); end
% 2.2 - When input data is X1,Y1,X2,Y2,..., searches all inputs and
% when a non-numerical is encountered all remaining input is
% considered a property modifer inputs
else
for i = 1:nargin; n(i) = isnumeric(varargin{i}); end
idx = find(n==0,1);
if isempty(idx); C1 = varargin;
else C1 = varargin(1:idx-1); opt = varargin(idx:nargin);
end
end
% 3 - SEPERATE THE PROPERTY MODIFIERS AND SECONDARY AXIS DATA
[a,C2] = subfunction_options(opt);
% 4 - PREPARE DATA FOR PLOTTING
[X1,Y1] = subfunction_preparedata(C1,a);
[X2,Y2] = subfunction_preparedata(C2,a,'secondary');
% 5 - PLOT DATA
% 5.1 - Create new plots
if isempty(a.append) || ~ishandle(a.append);
[fig,ax] = subfunction_plotdata(X1,Y1,X2,Y2,a);
% 5.2 - Append data to existing data
else
[fig,ax] = subfunction_appenddata(X1,Y1,X2,Y2,a);
end
% 6 - BUILD SLIDERS, LIMITBOXES, AND CONTEXT MENUS
% 6.1 - Build controls and figure menu (don't build when appending)
if isempty(a.append) || ~ishandle(a.append);
subfunction_builduicontrols(fig,ax)
subfunction_figuremenu(fig);
end
% 6.2 - Build line context menus
subfunction_linemenu(findobj(fig,'Type','line'));
% 7 - APPLY SETTINGS AND PLOT LIMITS
% 7.1 - Place figure settings in guidata for figure
% a = guidata(gcf); % Collect latest guidata
a.figure = fig; % Add the figure handle
guidata(fig,a); % Update guidata
% 7.2 - Build list of items to intilize
A = {'xlimit','ylimit','y2limit','xzoom','yzoom','colormap',...
'y2zoom','xstepbox','ystepbox','y2stepbox',...
'xgrid','ygrid','y2grid','xdir','ydir','y2dir',...
'intlab','legend','legend2','editfont','interpreter',...
'genericline','linespec','linespec2','facecolor',...
'units','size','windowstyle','annotation',...
'yci','xci','y2ci','x2ci','menubar','xminorgrid',...
'yminorgrid','xminortick','yminortick','resizelegend',...
'tight'};
% 7.3 - Apply axis limits
subfunction_limits;
% 7.4 - Set absolulte limits of data
subfunction_sliderlimits;
% 7.5 - Apply setup function to setup figure
for i = 1:length(A); setup(fig,'initilize',A{i}); end
% 7.6 - Export figure if desired;
if ~isempty(a.exportfile); callback_export(fig,[]); end
%--------------------------------------------------------------------------
% SUBFUNCTION: subfunction_options
function [a,C2] = subfunction_options(in)
% SUBFUNCTION_OPTIONS seperates user inputed property modifiers
n = length(in);
% 1 - SET THE DEFAULT SETTINGS
a.advanced = struct([]);
a.addto = 1;
a.annotation = '';
a.annotationbackground = 'none';
a.annotationbox = 'k';
a.annotationcoord = [];
a.annotationlabel = '';
a.append = [];
a.area = 'off';
a.axescolor = [0.94,0.94,0.94];
a.caxis = [];
a.colorbar = 'off';
a.colorbarlabel = '';
a.colormap = 'gray';
a.colororder = lines(10);
a.contour = 'off';
a.contourfill = 'on';
a.contourlinecolor = 'none';
a.contourxunits = 1;
a.contouryunits = 1;
a.dateform = 'mm/dd/yy HH:MM';
a.interpreter = 'none';
a.exportfile = ''; a.exportpath = cd;
a.facecolor = '';
a.fontname = 'Arial'; a.fontweight = 'normal';
a.fontsize = 10; a.fontunits = 'points'; a.fontangle = 'normal';
a.legend = NaN; a.legend2 = NaN;
a.linespec = {}; a.linespec2 = {};
a.linestyle = '-'; a.linestyle2 = '--';
a.linewidth = 1; a.linewidth2 = 1;
a.linestyleorder = '';
a.load = [];
a.logx = 'off'; a.logy = 'off';
a.location = 'NorthWest'; a.location2 = 'NorthEast';
a.menubar = 'figure';
a.marker = 'none'; a.marker2 = 'none';
a.markersize = 2; a.markersize2 = 2;
a.name = '';
a.prompt = 'on';
a.resizelegend = 'off';
a.save = [];
a.secondary = {};
a.size = [7,5];
a.tight = 'off';
a.title = '';
a.units = 'inches';
a.windowstyle = 'normal';
a.xci = []; a.yci = []; a.x2ci = []; a.y2ci = [];
a.xcicolor = {}; a.ycicolor = {}; a.x2cicolor = {}; a.y2cicolor = {};
a.xciwidth = 0.01; a.yciwidth = 0.01;
a.xdatetick = 'off'; a.ydatetick = 'off'; a.y2datetick = 'off';
a.xdir = 'normal'; a.ydir = 'normal'; a.y2dir = 'normal';
a.xgrid = 'on'; a.ygrid = 'on'; a.y2grid = 'off';
a.xlabel = NaN; a.ylabel = NaN; a.y2label = NaN;
a.xlim = []; a.ylim = []; a.y2lim = [];
a.xlimit = 'off'; a.ylimit = 'off'; a.y2limit = 'off';
a.xminorgrid = 'off'; a.yminorgrid = 'off'; a.y2minorgrid = 'off';
a.xminortick = 'off'; a.yminortick = 'off'; a.y2minortick = 'off';
a.xstep = []; a.ystep = []; a.y2step = [];
a.xstepbox = 'off'; a.ystepbox = 'off'; a.y2stepbox = 'off';
a.xtick = []; a.ytick = []; a.y2tick = [];
a.xticklabel = {}; a.yticklabel = {}; a.y2ticklabel = {};
a.xtrim = []; a.ytrim = []; a.y2trim = [];
a.xzoom = 'off'; a.yzoom = 'off'; a.y2zoom = 'off';
% 2 - CHECK FOR APPEND OPTION (uses current plot structure for defaults)
idx = 1:2:length(in);
i = strmatch('append',lower(in(idx)));
if i ~= 0;
handle = in{idx(1)+1};
a = guidata(handle);
a.secondary = {}; a.advanced = struct([]);
end
% 3 - APPLY/LOAD DEFAULT SETTINGS
if ~ispref('xyscatter','default');
addpref('xyscatter','default',a);
else
A = getpref('xyscatter','default');
a = checkpref(A,a);
end
% 4 - SEPERATE THE DATA FROM OPTIONS
list = fieldnames(a); k = 1;
while k < n
% 4.1 - Get modifier tag, associated data, and initilize for next loop
opt = in{k}; value = in{k+1}; match = ''; k = k + 2;
% 4.2 - Apply the advanced option
if strcmpi(opt,'advanced');
b = value; fname = fieldnames(b);
for i = 1:length(fname); a.(fname{i}) = b.(fname{i}); end
end
% 4.3 - Load stored settings
if strcmpi(opt,'load');
if ~ispref('xyscatter',value);
warning('MATLAB:XYscatter:subfunction_options',...
'Desired settings do not exists, using default.');
else
A = getpref('xyscatter',value);
a = checkpref(A,a);
end
end
% 4.4 - Compare modifier tag with available options
if ischar(opt);
match = strmatch(lower(opt),lower(list),'exact');
if ~isempty(match); a.(list{match}) = value; end
end
% 4.5 - Produce an error message if modifier is not found
if isempty(match);
mes = ['The property modifier, ',opt,', was not recoignized.'];
disp(mes);
end
end
% 5 - RETURN SECONDARY AXIS DATA
C2 = a.secondary;
% 6 - SAVE SETTINGS, IF DESIRED
if ~isempty(a.save);
apply = a.save; a.save = []; a.secondary = [];
if ~ispref('xyscatter',apply);
addpref('xyscatter',apply,a);
else
ques = ['The settings, ',apply,' already exist, do you ',...
'want to overwrite?'];
q = questdlg(ques,'Overwrite?','Yes');
if strcmpi(q,'yes'); setpref('xyscatter',apply,a); end
end
end
%--------------------------------------------------------------------------
% SUBFUNCTION: options >> checkpref
function A = checkpref(A,a)
% CHECKPREF checks that loaded pref's have the needed fields
fn = fieldnames(a);
for i = 1:length(fn);
if ~isfield(A,fn{i}); A.(fn{i}) = a.(fn{i}); end
end
%--------------------------------------------------------------------------
% SUBFUNCTION/CALLBACK: setup
function setup(hObject,eventdata,varargin)
% SETUP applies user figure settings
% 1 - SET INTILIZATION CASE (trig == 1 if initilizing the figure)
trig = 0;
if ischar(eventdata) && strcmpi(eventdata,'initilize'); trig = 1; end
% 2 - COLLECT FIGURE WINDOW HANDLES AND SETTINGS
a = guidata(hObject);
h = guihandles(hObject);
% 3 - FIND AXIS HANDLES, CORRECTING FOR EXISTANCE OF A LEGEND
ax1 = findobj(gcf,'YAxisLocation','left','-not','tag','legend',...
'-not','tag','resizelegend');
ax2 = findobj(gcf,'YAxisLocation','right','-not','tag','legend',...
'-not','tag','resizelegend');
% 4 - SET VISIBILITY OF SECONDARY Y-AXIS MENU
if isempty(ax2);
set(h.y2menu,'visible','off');
set(h.y2stepbox_tool,'visible','off');
set(h.y2limit_tool,'visible','off');
else
set(h.y2menu,'visible','on');
set(h.y2stepbox_tool,'visible','on');
set(h.y2limit_tool,'visible','on');
end
item = varargin{1};
switch lower(item)
% 5 - SET WINDOWSTYLE OPTION
case {'windowstyle'}; set(gcf,'WindowStyle',a.windowstyle);
% 6 - SET VISIBILITY OF BOXES AND SLIDERS
case {'xlimit','ylimit','y2limit','xzoom','yzoom','y2zoom',...
'xstepbox','ystepbox','y2stepbox'}
% 6.1 - Change the checkbox status
a.(item) = toggle(h.(item),a.(item),trig);
% 6.2 - Set visibility of associated objects
user = get(h.(item),'UserData');
for i = 1:length(user);
set(h.(user{i}),'Visible',a.(item));
end
% 7 - TOGGLE GRID LINES ON/OFF
% 7.1 - Major grid lines
case {'xgrid','ygrid','y2grid'};
% 7.1.1 - Change the checkbox status
a.(item) = toggle(h.(item),a.(item),trig);
% 7.1.2 - Set the status of the grid
set(ax1,'XGrid',a.xgrid);
set(ax1,'YGrid',a.ygrid);
if ~isempty(ax2); set(ax2,'YGrid',a.y2grid); end
% 7.2 - Minor grid lines
case {'xminorgrid','yminorgrid','y2minorgrid'}
a.(item) = toggle(h.(item),a.(item),trig);
set(ax1,'XMinorGrid',a.xminorgrid);
set(ax1,'YMinorGrid',a.yminorgrid);
if ~isempty(ax2); set(ax2,'YMinorGrid',a.y2minorgrid); end
% 7.3 - Minor tick marks
case {'xminortick','yminortick','y2minortick'}
a.(item) = toggle(h.(item),a.(item),trig);
set(ax1,'XMinorTick',a.xminortick);
set(ax1,'YMinorTick',a.yminortick);
if ~isempty(ax2); set(ax2,'YMinorTick',a.y2minortick); end
% 8 - TOGGLE THE AXIS DIRECTION
case {'xdir','ydir','y2dir'}
% 8.1 - Change the checkbox status
if trig == 0;
new = toggle(h.(item),get(h.(item),'Checked'),trig);
else
if strcmpi(a.(item),'reverse');
set(h.(item),'Checked','on');
new = 'on';
else new = 'off';
end
end
% 8.2 - Extablish axis and limit boxes to operate on
switch item
case 'xdir'; oper = 'XDir'; ax = ax1;
case 'ydir'; oper = 'YDir'; ax = ax1;
case 'y2dir'; oper = 'YDir'; ax = ax2;
end
% 8.3 - Reverse axis and limit box tags
switch new
case 'on'; set(ax,oper,'reverse'); a.(item) = 'reverse';
case 'off'; set(ax,oper,'normal'); a.(item) = 'normal';
end
% 9 - INSERT AXIS LABELS
case {'setlabels','intlab'}
% 9.1 - Update the axis labels
a = getaxislabels(a,ax1,ax2,item);
% 9.2 - Set primary axis labels
xlabel(ax1,a.xlabel); ylabel(ax1,a.ylabel);
title(ax1,a.title); set(gcf,'Name',a.name);
% 9.3 - Set secondary axis labels
if ishandle(ax2); ylabel(ax2,a.y2label); end
% 9.4 - Add menu items for editing individual test items
htxt = unique(findall(gcf,'Type','Text'));
subfunction_textmenu(htxt);
% 10 - INSERT PRIMARY AXIS LEGEND
case {'legend'}
if isempty(a.legend); legend(ax1,'off'); return;
% elseif trig == 0;
% a.legend = addlegend(ax1,NaN,'Y-axis',a.location);
else
a.legend = addlegend(ax1,a.legend,'Y-axis',a.location);
end
% 11 - INSERT SECONDARY AXIS LEGEND
case {'legend2'}
if isempty(a.legend2); legend(ax2,'off'); return;
elseif isempty(a.secondary); return;
% elseif trig == 0;
% a.legend2 = addlegend(ax2,NaN,'Y2-axis',a.location2);
else
a.legend2 = addlegend(ax2,a.legend2,'Y2-axis',a.location2);
end
% 12 - ADJUST UNITS/LATEX SUB-MENU TOGGLES
case {'interpreter'}
% 12.1 - Update check mark and selection
item = a.interpreter;
% 12.1.1 - Case when intilizing
if trig == 1; set(h.(item),'Checked','on'); item = 'tex';
% 12.1.2 - Case when selected from menu
else
c = get(get(h.(item),'Parent'),'Children');
set(c,'Checked','off');
set(hObject,'Checked','on');
a.interpreter = get(hObject,'tag');
end
% 12.2 - Apply settings
h1 = findobj(gcf,'-property','Interpreter');
h2 = findall(gcf,'Interpreter',item);
set([h1;h2],'Interpreter',a.interpreter);
% 13 - CHANGE FONT ATTRIBUTES
case {'editfont'}
% 13.1 - Case when not-initializing
if trig == 0;
s = uisetfont(a);
if isstruct(s); fn = fieldnames(s);
for i = 1:length(fn); a.(lower(fn{i})) = s.(fn{i}); end
end,end
% 13.2 - Get handles of figure properties
if length(varargin) == 2 && ishandle(varargin{2});
handles = varargin{2};
else
h1 = findobj(gcf,'-property','fontsize','-not',...
'Type','uicontrol');
h2 = findall(gcf,'Type','Text');
handles = [h1;h2];
end
% 13.3 - Set the font properties
set(handles,'FontName',a.fontname,'FontWeight',a.fontweight,...
'FontSize',a.fontsize,'FontUnits',a.fontunits,...
'FontAngle',a.fontangle);
% 14- CHANGE/SET AXIS COLOR
case {'axescolor'}
% 14.1 - Prompt user for color if called from menu
if trig == 0; c = colorui;
else c = a.axescolor;
end
% 14.2 - Set axis color
set(ax1,'Color',c);
% 15 - TOGGLE UNITS
case {'units'}
% 15.1 - Case when figure is being initilized
if trig == 1;
set(h.(lower(a.units)),'Checked','on');
% 15.2 - Case when called from figure context meun
else
h1 = get(h.resize,'Children');
set(h1,'Checked','off');
toggle(hObject,'off',trig);
a.units = get(hObject,'tag');
end
% 16 - SET FIGURE SIZE
case {'size'}
% 16.1 - Prompt user for new figure size
if trig == 0;
mes = {'Enter figure width:';'Enter figure height:'};
s = inputdlg(mes,'Figure size...',1,...
{num2str(a.size(1)),num2str(a.size(2))});
if isempty(s); return; end
a.size(1) = str2double(s{1}); a.size(2) = str2double(s{2});
end
% 16.2 - Set figure size
set(gcf,'Units',a.units)
P = get(gcf,'Position');
set(gcf,'Position',[P(1:2),a.size]);
set(gcf,'Units','Normalized');
% 17 - SET THE AXIS TO FIT TIGHT
case {'tight'}
% 17.1 - Check that status is on (case when intilizing figure)
if trig == 1 && strcmpi(a.tight,'off'); end
% 17.2 - Toggle off any text interpretation
h1 = findobj(gcf,'-property','Interpreter');
h2 = findall(gcf,'Interpreter',a.interpreter);
set([h1;h2],'Interpreter','none');
set(h.tight,'Checked','on');
% 17.3 - Gather axis information
tight = get(ax1,'TightInset');
tight2 = [0,0,0,0];
% 17.4 - Determine new axis limits
if ishandle(ax2); tight2 = get(ax2,'TightInset'); end
width = tight(3)+tight(1)+tight2(3)-tight2(1);
new = [tight(1),tight(2),1-width,1-tight(4)-tight(2)];
% 17.5 - Set new axis limits
set(ax1,'Position',new);
if ishandle(ax2); set(ax2,'Position',new); end
% 17.5 - Return the intepreter
set([h1;h2],'Interpreter',a.interpreter);
% 18 - SET THE LINESPEC OF PRIMARY AXIS
case {'linespec'}
if ishandle(ax1) && ~isempty(a.linespec)
p = flipdim(findobj(ax1,'Type','Line'),1);
if ~iscell(a.linespec); a.linespec = {a.linespec}; end
for i = 1:length(a.linespec); linespec(p(i),a.linespec{i}); end
end
% 19 - SET THE LINESPEC OF SECONDARY AXIS
case {'linespec2'}
if ishandle(ax2) & ~isempty(a.linespec2)
if ~iscell(a.linespec2); a.linespec2 = {a.linespec2}; end
p = flipdim(findobj(ax2,'Type','Line'),1);
if length(p) > 1; p = flipdim(p); end
for i = 1:length(a.linespec2); linespec(p(i),a.linespec2{i}); end
end
% 20 - SET THE COMPLETE GENERIC LINE PROPERTIES
case {'genericline'}
% 20.1 - Gather priarmy/secondary line handles
p1 = findobj(ax1,'Type','Line');
p2 = findobj(ax2,'Type','Line');
% 20.2 - Set appearance of primary and secondary lines, with the usage
% of LineStyleOrder option
if ~isempty(a.linestyleorder);
for i = 1:length(p1);
set(p1(i),'LineWidth',a.linewidth,...
'Marker',a.marker,'MarkerSize',a.markersize);
end
for i = 1:length(p2);
set(p2(i),'LineWidth',a.linewidth2,...
'Marker',a.marker2,'MarkerSize',a.markersize2);
end
end
% 20.3 - Set appearance of primary and secondary lines, with out usage
% of LineStyleOrder option
if isempty(a.linestyleorder);
for i = 1:length(p1);
set(p1(i),'LineWidth',a.linewidth,'LineStyle',a.linestyle,...
'Marker',a.marker,'MarkerSize',a.markersize);
end
for i = 1:length(p2);
set(p2(i),'LineWidth',a.linewidth2,'LineStyle',a.linestyle,...
'Marker',a.marker2,'MarkerSize',a.markersize2);
end
end
% 21 - ADD TEXTARROW ANNOTATION
case {'annotation'}
if trig == 1 && isempty(a.annotation); return; end
annotatefigure([],[],a.annotation);
a.annotation = ''; a.annotationcoord = []; a.annotationlabel = '';
guidata(gcf,a);
% 22 - CHANGE THE COLORMAP
case 'colormap';
colormap(a.colormap);
% 23 - SET FACECOLOR FOR STACKED AREA PLOT
case 'facecolor';
if ~isempty(a.facecolor);
% 23.1 - Gather area hobjects
hobj = findobj(ax1,'Type','hggroup');
% 23.2 - Set the color of the objects
for i = 1:length(hobj);
set(hobj(i),'FaceColor',a.facecolor{i});
end
end
% 24 - PLOT THE CONIFIDENCE LEVEL INTERVALS
case {'xci','yci'};
if ~isempty(a.(item)); subfunction_plotci(item); end
% 25 - SET FIGURE MENU VALUE
case 'menubar'
set(gcf,'MenuBar',a.menubar);
% 26 - RESIZE LEGEND
case 'resizelegend'
if trig == 1 && strcmpi(a.(item),'off'); return; end
resizelegend(gcf);
end
% UPDATE THE FIGURE GUIDATA
guidata(gcf,a);
%-------------------------------------------------------------------------
% SETUP >> SUBFUNCTION: annotatefigure
function annotatefigure(~,~,type)
% ANNOTATEFIGURE adds a text, line, etc. annotation to the figure
% 1 - CHECK FOR INITILIZATION CONDITIONS
% 1.1 - Gather axis and figure informaion
ax1 = findobj(gcf,'YAxisLocation','left','-not','tag','legend');
set(gcf,'CurrentAxes',ax1);
a = guidata(gcf);
% 1.2 - If a single value is entered convert to a cell
if ~iscell(type);
type = {type};
coord = {a.annotationcoord}; text = {a.annotationlabel};
elseif iscell(type);
coord = a.annotationcoord; text = a.annotationlabel;
end
% 2 - DETERMINE THE TYPE OF ANNOTATION BEING ADDED
for i = 1:length(type); % Loops through mulitiple annotation entries
switch lower(type{i})
case {'textarrow'}
mes = ['(1) select the location of the text box, ',...
'then (2) the location for placing arrow.'];
opt = {'TextBackgroundColor','TextEdgeColor'};
txt = 'on'; C = 2;
case {'line','arrow','doublearrow'}
mes = ['(1) select the start location of the line, ',...
'then (2) the location for placing arrow.'];
txt = 'off'; C = 2;
case {'textbox','ellipse','rectangle'}
mes = ['Select the lower-left location of the text. '];
opt = {'BackgroundColor','EdgeColor'};
txt = 'on'; C = 1;
end
% 3 - DETERMINE LOCATIONS
% 3.1 - Prompt user if not coordinates exist
if isempty(coord{i})
m = msgbox(mes,'Add text label...','Help','Modal');
uiwait(m); str = '';
[x,y] = ginput(C); % x- and y- coordinate of clicks
% 3.2 - Use user supplied input
else
str = text{i}; x = coord{i}(:,1); y = coord{i}(:,2);
end
% 4 - CALCULATE THE LOCATION, NORMALIZED TO FIGURE SIZE OF ARROW
% 4.1 - Get axis limits
X = get(gca,'XLim'); % x-axis limits
Y = get(gca,'YLim'); % y-axis limits
p = get(gca,'Position'); % Axis position (normalized)
% 4.2 - Normlize the given inputs
x = p(1) + ((x-X(1))./(X(2)-X(1))).*p(3); % x-coordinate
y = p(2) + ((y-Y(1))./(Y(2)-Y(1))).*p(4); % y-coordinate
% 4.3 - Build proper input
if C == 2; input = {x,y};
elseif C == 1; input = {[x,y,0,0]};
end
% 5 - PROMPT USER FOR TEXT STRING (IF NOT INPUTED)
if isempty(str) && strcmpi(txt,'on');
str = inputdlg('Enter text for current label:','Add text...');
if isempty(str); return; end
str = str{1};
end
% 6 - PLACE TEXTARROW ANNOTATION ON FIGURE
ha = annotation(type{i},input{:});
if strcmpi(txt,'on');
set(ha,'String',str,opt{1},a.annotationbackground,...
opt{2},a.annotationbox);
setup(gcf,'initilize','editfont');
setup(gcf,'initilize','interpreter');
subfunction_textmenu(ha);
end
% Ends loop for mulitple inputs (Sec.2)
end
%--------------------------------------------------------------------------
% SUBFUNCTION: setup >> linespec
function linespec(handle,spec)
% LINESPEC sets the linesytle, marker, and color of a line
% 1 - SET POSSIBLE OPTIONS
A{1} = {'-',':','--','-.'};
A{2} = {'+','o','*','.','x','s','d','^','v','>','<','p','h'};
A{3} = {'r','g','b','c','m','y','k','w'};
% 2 - FIND MATCHING VALUES
for i = 1:length(A); [m{i},spec] = findvalue(A{i},spec); end
% 3 - APPLY THE SETTINGS
set(handle,'LineStyle',m{1});
set(handle,'Marker',m{2});
if ~strcmpi(m{3},'none'); set(handle,'Color',m{3}); end
%--------------------------------------------------------------------------
% SUBFUNCTION: setup >> linespec >> findvalue
function [match,spec] = findvalue(test,spec)
% FINDVALUE searchs a linespec string and returns the matched paramter
match = 'none';
for i = 1:length(test);
[~,~,~,mat] = regexp(spec,test{i});
if ~isempty(mat) && strcmpi(mat{1},test{i}); match = mat{1}; end
end
spec = regexprep(spec,match,'');
%--------------------------------------------------------------------------
% SETUP >> SUBFUNCTION: addlegend
function leg = addlegend(ax,leg,Lab,loc)
% ADDLEGEND places a legend on the current figure and prompts if needed
% 1 - GATHER LINE INFORMATION
% 1.1 - Determine the type for gathering legend names
a = guidata(gcf); type = 'line';
if strcmpi(a.area,'on'); type = 'hggroup'; end
% 1.2 - Get line info and exit if it does not exist
hline = flipdim(findobj(ax,'Type',type),1);
if isempty(hline); return; end
name = cellstr(get(hline,'DisplayName'));
% 2 - IF NaN IS USED PROMPT THE USER
if isnumeric(leg)
for i = 1:length(name);
new{i} = ['Data ',num2str(i)];
prompt{i} = ['Enter name for line #',num2str(i),':'];
end
if ~strcmpi(a.prompt,'off');
new = inputdlg(prompt,[Lab,'...'],1,name);
end
if isempty(new); leg = name; else leg = new; end
end
% 3 - BUILD LEGEND
% 3.1 - Check that "leg" is a cell array
if ischar(leg); leg = {leg}; end
% 3.2 - Check that "leg" is oriented vertically
if size(leg,1) == 1; leg = leg'; end
% 3.3 - Append legend entries
if length(hline) > length(leg);
n = length(name); m = length(leg);
leg = [name(1:n-m);leg];
end
% 3.4 - Build legend
if length(leg) > length(hline);
disp('Extra legend entries omitted.');
leg = leg(1:length(hline));
end
set(hline,{'DisplayName'},leg);
legend(ax,leg,'Location',loc);
%--------------------------------------------------------------------------
% SETUP >> SUBFUNCTION: getaxislabels
function a = getaxislabels(a,ax1,ax2,item)
% GETAXISLABELS prompts user for axis and figure labels
% 1 - SEARCH FOR EXISTING LABELS
% 1.1 - Search primary axis labels and title
M = {'xlabel','ylabel','Title'};
for i = 1:length(M);
e = get(get(ax1,M{i}),'String');
if ~isempty(e); a.(M{i}) = e; end
end
% 1.2 - Search secondary axis labels
if ishandle(ax2);
e = get(get(ax2,'YLabel'),'String');
if ~isempty(e); a.y2label = e; end
end
% 2 - DETERMINE THE PROMPT STATUS
% Based on default NaN values if the value is a numeric set that will
% trigger prompting and also set the default string to a blank value
% for use when prompting or use the input value
N = {a.xlabel,a.ylabel,a.y2label,a.title,a.name};
for i = 1:length(N);
val(i) = isnumeric(N{i});
if val(i) == 1; def{i} = ''; else def{i} = N{i}; end
end
% 3 - SET A TRIGGER FOR PROMPTING USER TO ENTER INFORMATION
t = '';
if strcmpi(item,'setlabels'); t = 'prompt';
elseif ishandle(ax2) & sum(val(1:3)) > 0; t = 'prompt';
elseif sum(val(1:2)) > 0; t = 'prompt';
end
% 4 - EXIT IF THE USER IS NOT TO BE PROMPTED
if strcmpi(a.prompt,'off'); return; end
if ~strcmpi(t,'prompt'); return; end
% 5 - BUILD THE PROMPT LIST
list = {'Enter the X-axis label:',...
'Enter the primary Y-axis label:',...
'Enter the plot title:',...
'Enter figure window name:'};
defstr = {def{1},def{2},def{4},def{5}};
if ishandle(ax2);
new = 'Enter the secondary Y-axis label:';
list = [list(1:2),new,list(3:4)]; defstr = def;
end
% 6 - PROMPT USER TO ENTER LABEL ITEMS
c = inputdlg(list,'Enter labels...',1,defstr);
if isempty(c) && ~strcmpi('setlabels',item);
for i = 1:length(list); c{i} = ''; end;
elseif isempty(c); return;
end
% 7 - ENTER USER INPUTED STRINGS INTO DATA STORAGE ARRAY
if length(list) == 4;
a.xlabel = c{1}; a.ylabel = c{2}; a.title = c{3}; a.name = c{4};
elseif length(list) == 5;
a.xlabel = c{1}; a.ylabel = c{2};
a.y2label = c{3}; a.title = c{4}; a.name = c{5};
end
%--------------------------------------------------------------------------
% SETUP >> SUBFUNCTION: toggle
function chk = toggle(h,chk,trig,varargin)
% TOGGLE changes the checked status of a menu item
if trig == 0;
switch chk;
case 'on'; chk = 'off';
case 'off'; chk = 'on';
end
end
set(h,'Checked',chk);
%--------------------------------------------------------------------------
% SUBFUNCTION: rebuildlegend
function callback_restorelegend(hObject,~)
h = findobj('Tag','resizelegend');
delete(h);
setup(hObject,[],'legend');
setup(hObject,[],'legend2');
%--------------------------------------------------------------------------
% SUBFUNCTION: resizelegend
function resizelegend(fig)
% RESIZELEGEND creates a resizeable legend
% 1 - GATHER HANDLES FOR LEGENDS
h = findall(fig,'Tag','legend');
% 2 - BUILD THE NEW LEGENDS
for i = 1:length(h);
% 2.1 - Gather the legend information
U = get(h(i),'UserData');
% 2.2 - Create a new legend axis
p = get(h(i),'Position');
new = axes('parent',fig,'box','on','xtick',[],'ytick',[],'XColor','w',...
'YColor','w','color',[1,1,1],'Position',p,'XLimMode','manual',...
'YLimMode','manual','HandleVisibility','callback',...
'tag','resizelegend');
% 2.3 - Seperate the legend information for each entry
N = length(U.handles); k = 1;
LHtext = U.LabelHandles(1:N);
LHline = U.LabelHandles(N+1:2:end);
for j = 1:N;
C(j).LabelHandles = [LHline(j),LHtext(j)]; k = k + 3;
C(j).lstrings = U.lstrings{j};
C(j).handles = U.handles(j);
end
% 2.4 - Construct the new legend
N = length(C);
for j = 1:N;
W(j) = buildline(C(j),new);
end
% 2.5 - Resize and remove the new legend
P = get(new,'Position');
P(3) = P(3)*max(W);
set(new,'Position',P);
% 2.6 - Store legend information in the lines userdata
user = get(U.handles(i),'UserData');
user.legend = C(i);
set(U.handles(i),'UserData',user);
% 2.7 - Add a box
x = [0,1,1,0,0];
y = [0,0,1,1,0];
hold on;
plot(new,x,y,'-k');
end
delete(h);
%--------------------------------------------------------------------------
% RESIZELEGEND >> SUBFUNCTION: buildline
function W = buildline(C,new)
% BUILDLINE creates a new legend entry
% 1 - GATHER INFORMATION ABOUT THE CURRENT LEGEND ENTRY
hline = C.LabelHandles(1);
txt = C.LabelHandles(2);
x = get(hline,'Xdata');
y = get(hline,'Ydata');
e = get(txt,'Extent');
p = get(txt,'Position');
% 2 - DEFINE THE NEW POSITIONS FOR THE ENTRY
sep = 0.05;
% len = (x(2) - x(1))*0.75;
% x = [sep,len];
% p(1) = x(2);
W = p(1) + e(3)-2*sep;
% 3 - ASSIGN THE ENTRY TO THE NEW LEGEND
set(hline,'Parent',new,'Xdata',x,'Ydata',y);
set(txt,'Parent',new,'Position',p,'VerticalAlignment','Middle',...
'HorizontalAlignment','left','HitTest','off');
%--------------------------------------------------------------------------
% SUBFUNCTION: subfunction_preparedata
function [X,Y] = subfunction_preparedata(c,a,varargin)
% SUBFUNCTION_PREPAREDATA organizes and trims inputed data for plotting.
% This function changes a cell array input into X,Y matrices for plotting
% c = {X1,Y1,X2,Y2,...,Xn,Yn} pairs just as in MATLAB's plot. The input
% arrays are filled with NaN so that X = [X1,X2,...,Xn] is a valid
% operatation and similarly for Y.
% - The data MUST be organized in columns.
% - Is able to handle X,Y1,Y2,Y3
%
% This function also trims the data according to user specified property
% modifier.
%__________________________________________________________________________
% 0 - RETURN IN INPUT IS EMPTY
if isempty(c); X = []; Y = []; return; end
% 1 - DETERMINE THE LARGEST MATRIX
for i = 1:length(c); n(i) = size(c{i},1); end
% 2 - FILL MATRICES WITH NaN
N = max(n); % Maximum length of any data column
X = []; Y = []; % Initilize output
for i = 2:2:length(c); % Loop through each data pair
% 2.1 - Seperates the data pairs
x = c{i-1}; % Seperate x-data
y = c{i}; % Seperate y-data
% 2.2 - Copies single columns for data column available, i.e. the
% case when x = 1 column and y = mulitple columns. The x data must
% be reproduced for every y column so when the ouput is constructed
% in 2.4 the pairs will be of equal size.