-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathSHeatmap.m
More file actions
2719 lines (2543 loc) · 144 KB
/
Copy pathSHeatmap.m
File metadata and controls
2719 lines (2543 loc) · 144 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
classdef SHeatmap < handle
% SHeatmap Create and customize heatmaps with various cell shapes format
% and triangular type
% SHM = SHeatmap(data); creates a heatmap from a numerical matrix with
% default square cells.
% 从数值矩阵创建默认方形单元格热图。
%
% SHM = SHeatmap(ax, ___); creates the heatmap in the specified axes.
% 在指定坐标区创建热图。
%
% SHM = SHeatmap(___, propName, propVal); specifies property name-value
% pairs when creating the object.
% 创建对象时指定属性名-属性值对。
%
% SHM.propName = propVal; sets properties before calling draw().
% 在调用 draw() 前设置属性。
%
% SHM = SHM.draw(); renders the heatmap.
% 渲染热图。
%
% Basic usage:
% Data = rand(5, 15);
% SHM = SHeatmap(Data);
% SHM.draw();
%
% Format:
% Data = rand(15, 15) - .5;
% SHM = SHeatmap(Data, 'Format','donut');
% SHM.draw();
%
% 'sq' : square (default) : 方形 (默认)
% 'shade' : Square (neg-values shaded): 方形 (负数部分阴影填充)
% 'rrect' : rounded rectangle : 圆角矩形
% 'c2rect' : circle to rectangle : 圆形到矩形过度
% 'pie' : pie chart : 饼图
% 'donut' :donut chart : 环形饼图 (甜甜圈图)
% 'circ' : circle : 圆形
% 'bcirc' : circle with box : 有边框的圆形
% 'oval' : oval : 椭圆形
% 'hex' : hexagon :六边形
% 'star' : star : 五角星
% 'arrow' : arrow : 箭头
% 'trill'(tril) : lower left triangle : 下三角
% 'triur'(triu) : upper right triangle : 上三角
% 'trilr' : lower right triangle : 右下三角
% 'triul' : upper left triangle : 左上三角
% 'asq' : auto-size square :自带调整大小的方形
% 'acirc' : auto-size circular :自带调整大小的圆形
% 'arrect' : auto-size rounded rect : 自带调整大小的圆角矩形
% 'txt'(text) : colored text : 带颜色的文本
% 'cust' : custom shape : 自定义形状
% 'acust' : auto-size custom shape : 自带调整大小的自定义形状
%
% Type:
% X = randn(20, 15) + [(linspace(-1, 2.5, 20)').*ones(1, 6), ...
% (linspace(.5, -.7, 20)').*ones(1, 5), ...
% (linspace(.9, -.2, 20)').*ones(1, 4)];
% Data = corr(X);
% SHM = SHeatmap(Data, 'Type','sq').draw();
% SHM.setType('triu');
%
% 'triu' : upper triangle : 上三角部分
% 'tril' : lower triangle : 下三角部分
% 'triu0' : upper triangle without diagonal : 扣除对角线上三角部分
% (strictly upper triangular part) : (严格上三角)
% 'tril0' : lower triangle without diagonal : 扣除对角线下三角部分
% (strictly lower triangular part) : (严格下三角)
% 'linkl' : lower triangle for mantel links : 适配 mantel 链接的下三角
% 'linku' : upper triangle for mantel links : 适配 mantel 链接的上三角
% 'row' : show row labels & ticks only : 仅显示行标签及行刻度
% 'col' : show col labels & ticks only : 仅显示列标签及列刻度
% 'varu' : upper triangle with diagonal : 上三角部分
% cells replaced by variable names : 对角线使用变量名标签替换
% 'varl' : lower triangle with diagonal : 下三角部分
% cells replaced by variable names : 对角线使用变量名标签替换
%
% Methods: (try: help SHeatmap.setText)
% draw - Render the heatmap object (渲染热图对象)
% setType - Adjust display to show only triangular part of the matrix (仅展示矩阵的三角部分)
% setVarName - Assign variable names to rows and columns (为行和列分配变量名)
% setRowName - Assign variable names to rows (为行分配变量名)
% setColName - Assign variable names to cols (为列分配变量名)
% setRowGroupName - Assign group names to row-groups (为行分组分配组名)
% setColGroupName - Assign group names to col-groups (为列分组分配组名)
% setText - Show value labels with auto-contrast color, and set properties (显示数值标签并自动调整颜色,设置标签属性)
% setTextFormat - Apply a custom formatting function to all value labels (对数值标签应用自定义格式化函数)
% setFontName - Set the font name of all existing labels (设置所有已绘制标签的字体名称)
% showStars - Overlay significance stars on value labels based on p-values (根据 p 值在数值标签上叠加显著性星标)
% setBox - Set properties for box handle (设置框样式)
% setGrid - Set properties for grid handle (设置网格样式)
% setFrame - Set properties for frame and tick handle (设置外轮廓样式)
% setPatch - Set properties for all patch objects (为所有填充图形设置属性)
% setRowLabel - Set properties for all row label text objects (设置所有行标签的属性)
% setColLabel - Set properties for all col label text objects (设置所有列标签的属性)
% setRowGroupLabel - Set properties for all row-group label text objects (设置所有行分组标签的属性)
% setColGroupLabel - Set properties for all col-group label text objects (设置所有列分组标签的属性)
% setRowLabelLocation - Move row labels to specified location (设置行标签位置)
% setColLabelLocation - Move col labels to specified location (设置列标签位置)
% setRowGroupLabelLocation - Move row-group labels to specified location (设置行分组标签位置)
% setColGroupLabelLocation - Move col-group labels to specified location (设置行分组标签位置)
% freezeColors - Permanently assign the current colormap colors to each patch
% based on its value, decoupling them from both the
% colormap axis limits (CLim) and the colormap itself
% (根据当前数值将颜色映射固定到每个填充图形,使其不再随颜色轴范围或颜色映射表的变化而改变)
% setXYTLim - Set X, Y, and Theta limits for the heatmap (设置热图 X轴、 Y轴、角度范围)
% =========================================================================
% @author : slandarer
% 公众号 : slandarer随笔
% -------------------------------------------------------------------------
% Zhaoxu Liu / slandarer (2025). special heatmap
% (https://www.mathworks.com/matlabcentral/fileexchange/125520-special-heatmap),
% MATLAB Central File Exchange. 检索来源 2025/12/1.
% =========================================================================
properties
ax,
Parent = [];
arginList = {'Parent', 'Format', 'SData', 'Type', ...
'VarName', 'RowName', 'ColName', ...
'GroupSep', 'RowGroup', 'ColGroup', ...
'TickLength','TickLabelOffset','GroupLabelOffset', ...
'RowGroupName', 'ColGroupName', 'ShapeFlipX', 'ShapeFlipY'}
Data
PVal
Format = 'sq'
% 'sq' : square (default) : 方形 (默认)
% 'shade' : Square (neg-values shaded): 方形 (负数部分阴影填充)
% 'rrect' : rounded rectangle : 圆角矩形
% 'c2rect' : circle to rectangle : 圆形到矩形过度
% 'pie' : pie chart : 饼图
% 'donut' :donut chart : 环形饼图 (甜甜圈图)
% 'circ' : circle : 圆形
% 'bcirc' : circle with box : 有边框的圆形
% 'oval' : oval : 椭圆形
% 'hex' : hexagon :六边形
% 'star' : star : 五角星
% 'arrow' : arrow : 箭头
% 'trill'(tril) : lower left triangle : 下三角
% 'triur'(triu) : upper right triangle : 上三角
% 'trilr' : lower right triangle : 右下三角
% 'triul' : upper left triangle : 左上三角
% 'asq' : auto-size square :自带调整大小的方形
% 'acirc' : auto-size circular :自带调整大小的圆形
% 'arrect' : auto-size rounded rect : 自带调整大小的圆角矩形
% 'txt'(text) : colored text : 带颜色的文本
% 'cust' : custom shape : 自定义形状
% 'acust' : auto-size custom shape : 自带调整大小的自定义形状
SData = [.45, .21, .22, .09, .00, -.09, -.22, -.21, -.45, -.21, -.22, -.09, -.00, .09, 0.22, .21, .45
.00, .09, .22, .21, .45, .21, .22, .09, .00, -.09, -.22, -.21, -.45, -.21, -0.22, -.09, -.00];
ShapeFlipX = 'off'; % Horizontal flip of cell shapes (水平翻转单元格形状), 'on'/'off'
ShapeFlipY = 'off'; % Vertical flip of cell shapes (垂直翻转单元格形状), 'on'/'off'
Type = 'full';
% 'triu' : upper triangle : 上三角部分
% 'tril' : lower triangle : 下三角部分
% 'triu0' : upper triangle without diagonal : 扣除对角线上三角部分
% (strictly upper triangular part) : (严格上三角)
% 'tril0' : lower triangle without diagonal : 扣除对角线下三角部分
% (strictly lower triangular part) : (严格下三角)
% 'linkl' : lower triangle for mantel links : 适配 mantel 链接的下三角
% 'linku' : upper triangle for mantel links : 适配 mantel 链接的上三角
% 'row' : show row labels & ticks only : 仅显示行标签及行刻度
% 'col' : show col labels & ticks only : 仅显示列标签及列刻度
% 'varu' : upper triangle with diagonal : 上三角部分
% cells replaced by variable names : 对角线使用变量名标签替换
% 'varl' : lower triangle with diagonal : 下三角部分
% cells replaced by variable names : 对角线使用变量名标签替换
TickLength = .1; % Length of tick marks (刻度线长度)
TickLabelOffset = .25; % Offset distance from tick end to tick label (刻度末端到刻度标签的偏移距离)
GroupLabelOffset = 1.5; % Offset distance for group labels (分组标签的偏移距离)
RowLabelLocation = 'left'; % 'left', 'right', 'diag'
ColLabelLocation = 'bottom'; % 'top', 'bottom', 'diag'
RowGroupLabelLocation = 'left'; % 'left', 'right', 'diag'
ColGroupLabelLocation = 'bottom'; % 'top', 'bottom', 'diag'
Colormap; % Colormap (颜色映射表)
Colorbar; % Colorbar (颜色条)
% For a square matrix (e.g., corr(X) or correlation matrix of a single dataset):
VarName; % Variable names for the single dataset (变量名称)
% For a rectangular matrix (e.g., corr(X, Y) or cross-correlation between two datasets):
RowName; % Names of variables in dataset X (行变量名称)
ColName; % Names of variables in dataset Y (列变量名称)
RowGroup = []; % Row group assignments (行分组标签)
ColGroup = []; % Column group assignments (列分组标签)
GroupSep = .5; % Separation gap between groups (组间分离间距)
RowGroupName % Names for row groups (行分组名称)
ColGroupName % Names for column groups (列分组名称)
% X, Y, and Theta limits for the heatmap
XLim = [],
YLim = [],
TLim = [0, 0];
gridHdl % Grid handle (网格线句柄)
pieHdl; % Pie chart handle (饼图句柄)
patchHdl; % Patch handle (填充图形句柄)
textHdl; % Text (data value) handle (文本句柄)
boxHdl; % Outline handle (边框句柄)
frameHdl; % Frame (outline) handle (外轮廓句柄)
rowTickHdl; % Row tick handle (行刻度句柄)
colTickHdl; % Col tick handle (列刻度句柄)
rowLabelHdl; % Row label handle (行标签句柄)
colLabelHdl; % Column label handle (列标签句柄)
rowGroupLabelHdl % Row-group label handle (行分组标签句柄)
colGroupLabelHdl % Col-group label handle (列分组标签句柄)
end
properties (Hidden)
maxV
defaultCmp1 = [.97, .99, .94; .95, .98, .92; .92, .97, .90;
.90, .96, .88; .88, .95, .86; .86, .94, .83;
.84, .94, .81; .82, .93, .79; .79, .92, .77;
.75, .90, .75; .72, .89, .74; .68, .88, .72;
.64, .86, .72; .60, .84, .73; .55, .83, .75;
.51, .81, .76; .46, .79, .78; .41, .76, .79;
.37, .74, .81; .32, .71, .82; .28, .68, .81;
.25, .64, .79; .21, .60, .77; .18, .56, .75;
.14, .52, .73; .11, .49, .71; .07, .45, .69;
.04, .41, .68; .03, .37, .64; .03, .33, .59;
.03, .29, .55; .03, .25, .51];
defaultCmp2 = [.62, .00, .26; .69, .08, .28; .76, .16, .29;
.83, .24, .31; .87, .30, .30; .91, .36, .28;
.95, .42, .27; .97, .49, .29; .98, .58, .33;
.99, .66, .37; .99, .73, .42; .99, .79, .47;
1.0, .85, .52; 1.0, .90, .58; 1.0, .94, .65;
1.0, .98, .72; .98, .99, .72; .95, .98, .68;
.92, .97, .63; .87, .95, .60; .80, .92, .62;
.72, .89, .63; .64, .86, .64; .56, .82, .64;
.47, .79, .65; .39, .75, .65; .32, .67, .68;
.26, .60, .71; .20, .53, .74; .26, .45, .70;
.31, .38, .67; .37, .31, .64];
RP; CP; FX; FY; BX; BY; GX; GY;
SX = 1; SY = 1;
TxtShown = false;
XYTReset = false;
RGP; CGP; RGLST; CGLST;
PatchX; PatchY; PieX; PieY; nanTextHdl
end
methods
% =========================================================================
% Constructor: Create SHeatmap object (构造函数)
% =========================================================================
function obj = SHeatmap(varargin)
% Parse axes handle if provided (解析坐标区句柄)
if isa(varargin{1}, 'matlab.graphics.axis.Axes')
obj.ax = varargin{1};
obj.Parent = varargin{1};
varargin(1) = [];
else
% No axes provided
end
% Store data (存储数据)
obj.Data = varargin{1};
varargin(1) = [];
obj.maxV = max(max(abs(obj.Data)));
% Parse optional arguments (解析可选参数)
for i = 1:2:(length(varargin) - 1)
tid = ismember(lower(obj.arginList), lower(varargin{i}));
if any(tid)
obj.(obj.arginList{tid}) = varargin{i + 1};
end
end
% Choose colormap based on data sign (根据数据正负选择配色)
if any(any(obj.Data < 0))
obj.Colormap = obj.defaultCmp2;
else
obj.Colormap = obj.defaultCmp1(end:-1:1, :);
end
end
% =========================================================================
% Draw: Render the SHeatmap (渲染热图)
% =========================================================================
function varargout = draw(obj)
% obj.draw() - Render the heatmap object (渲染热图对象)
% mustBeAllowedFormat(obj.Format)
% mustBeAllowedColLabelLocation(obj.ColLabelLocation)
% mustBeAllowedRowLabelLocation(obj.RowLabelLocation)
% mustBeAllowedTriType(obj.Type)
% Set axes handle (设置坐标轴句柄)
if isempty(obj.Parent) && isempty(obj.ax)
obj.ax = gca;
else
obj.ax = obj.Parent;
end
% Configure axes properties (配置坐标轴属性)
obj.ax.NextPlot = 'add';
obj.ax.Box = 'on';
obj.ax.FontName = 'Times New Roman';
obj.ax.FontSize = 12;
obj.ax.LineWidth = 0.8;
obj.ax.YDir = 'reverse';
obj.ax.TickDir = 'out';
obj.ax.TickLength = [0.002, 0.002];
obj.ax.DataAspectRatio = [1, 1, 1];
obj.ax.XLim = [0.5, size(obj.Data, 2) + 0.5];
obj.ax.YLim = [0.5, size(obj.Data, 1) + 0.5];
obj.ax.YTick = 1:size(obj.Data, 1);
obj.ax.XTick = 1:size(obj.Data, 2);
% Apply colormap and colorbar (应用颜色映射和颜色条)
colormap(obj.ax, obj.Colormap);
obj.Colorbar = colorbar(obj.ax);
% Set color axis limits (设置颜色轴范围)
if any(any(obj.Data < 0))
try caxis(obj.ax, obj.maxV .* [-1, 1]), catch, end
try clim(obj.ax, obj.maxV .* [-1, 1]), catch, end
else
try caxis(obj.ax, obj.maxV .* [0, 1]), catch, end
try clim(obj.ax, obj.maxV .* [0, 1]), catch, end
end
obj.GroupSep(obj.GroupSep < 0) = 0;
obj.GroupSep(obj.GroupSep > 10) = 10;
obj.TickLength(obj.TickLength < 0) = 0;
obj.TickLength(obj.TickLength > .5) = .5;
obj.TickLabelOffset(obj.TickLabelOffset <= 1e-4) = 1e-4;
obj.TickLabelOffset(obj.TickLabelOffset > 1) = 1;
obj.GroupLabelOffset(obj.GroupLabelOffset <= 1e-4) = 1e-4;
obj.GroupLabelOffset(obj.GroupLabelOffset > 10) = 10;
if isempty(obj.RowGroup) || length(obj.RowGroup) < size(obj.Data, 1)
obj.RowGroup = ones(1, size(obj.Data, 1));
end
if isempty(obj.ColGroup) || length(obj.ColGroup) < size(obj.Data, 2)
obj.ColGroup = ones(1, size(obj.Data, 2));
end
obj.RowGroup = obj.RowGroup(1:size(obj.Data, 1));
obj.ColGroup = obj.ColGroup(1:size(obj.Data, 2));
obj.RowGroup = cumsum([1, diff(obj.RowGroup(:).') ~= 0]);
obj.ColGroup = cumsum([1, diff(obj.ColGroup(:).') ~= 0]);
obj.RP = (1:size(obj.Data, 1)) + (obj.RowGroup - 1).*obj.GroupSep;
obj.CP = (1:size(obj.Data, 2)) + (obj.ColGroup - 1).*obj.GroupSep;
obj.RGP = accumarray(obj.RowGroup(:), obj.RP(:), [], @mean);
obj.CGP = accumarray(obj.ColGroup(:), obj.CP(:), [], @mean);
obj.XLim = [obj.CP(1) - .5, obj.CP(end) + .5];
obj.YLim = [obj.RP(1) - .5, obj.RP(end) + .5];
obj.ax.XLim = [0.5, obj.CP(end) + 0.5];
obj.ax.YLim = [0.5, obj.RP(end) + 0.5];
obj.ax.XTick = obj.CP;
obj.ax.YTick = obj.RP;
obj.ax.XTickLabel = compose('%d', 1:size(obj.Data, 2));
obj.ax.YTickLabel = compose('%d', 1:size(obj.Data, 1));
% if ~isempty(obj.RowName)
% obj.ax.YTickLabel = obj.RowName;
% end
% if ~isempty(obj.ColName)
% obj.ax.XTickLabel = obj.ColName;
% end
% if ~isempty(obj.VarName)
% obj.ax.XTickLabel = obj.VarName;
% obj.ax.YTickLabel = obj.VarName;
% end
% Adjust figure size if needed (调整初始界面大小)
fig = obj.ax.Parent;
if strcmp(get(fig, 'Type'), 'figure')
fig.Color = [1, 1, 1];
if max(fig.Position(3:4)) < 600 && strcmp(fig.Units, 'pixels')
fig.Position(3:4) = [1.6, 1.8] .* fig.Position(3:4);
fig.Position(1:2) = fig.Position(1:2) ./ 4;
end
end
% Draw grid lines (绘制网格线)
obj.GX = nan; obj.GY = nan;
obj.gridHdl = plot(obj.ax, obj.GX, obj.GY, ...
'LineWidth', 0.8, 'Color', [0,0,0], 'LineStyle','--');
% Define base shape coordinates (定义基本形状坐标)
baseT = linspace(0, 2*pi, 100).';
hexT = linspace(0, 2*pi, 7).';
starT = linspace(0, 2*pi, 11).' + pi/10;
thetaMat = [1, 1; -1, 1].*sqrt(2)./2;
T4 = [linspace(0, pi/2, 20), linspace(pi/2, pi, 20), linspace(pi, 3*pi/2, 20), linspace(3*pi/2, 2*pi, 20)].';
X4 = [ones(1, 20), - ones(1, 20), - ones(1, 20), ones(1, 20)].';
Y4 = [ones(1, 20), ones(1, 20), - ones(1, 20), - ones(1, 20)].';
XA = [0; .5; .2; .2; -.2; -.2; -.5];
YA = [.5; 0; 0; -.5; -.5; 0; 0];
if strcmpi(obj.ShapeFlipX, 'on')
obj.SX = -1;
end
if strcmpi(obj.ShapeFlipY, 'on')
obj.SY = -1;
end
[cols, rows] = meshgrid(1:size(obj.Data, 2), 1:size(obj.Data, 1));
rows = reshape(obj.RP(rows), 1, []);
cols = reshape(obj.CP(cols), 1, []);
datas = reshape(obj.Data, 1, []);
mn = numel(obj.Data); sz = size(obj.Data);
tRatio = abs(datas)./obj.maxV;
switch lower(obj.Format)
case 'sq'
obj.PatchX = obj.SX.*repmat([-.5; .5; .5; -.5].*.98, [1, mn]) + repmat(cols, [4, 1]);
obj.PatchY = obj.SY.*repmat([-.5; -.5; .5; .5].*.98, [1, mn]) + repmat(rows, [4, 1]);
obj.patchHdl = fill(obj.ax, obj.PatchX, obj.PatchY, datas, 'EdgeColor','none');
obj.patchHdl = reshape(obj.patchHdl, sz);
case 'asq'
obj.PatchX = obj.SX.*repmat([-.5; .5; .5; -.5].*.98, [1, mn]).*repmat(tRatio, [4, 1]) + repmat(cols, [4, 1]);
obj.PatchY = obj.SY.*repmat([-.5; -.5; .5; .5].*.98, [1, mn]).*repmat(tRatio, [4, 1]) + repmat(rows, [4, 1]);
obj.patchHdl = fill(obj.ax, obj.PatchX, obj.PatchY, datas, 'EdgeColor','none');
obj.patchHdl = reshape(obj.patchHdl, sz);
case 'rrect'
obj.PatchX = obj.SX.*repmat((X4.*.7 + cos(T4).*.3).*.46, [1, mn]) + repmat(cols, [80, 1]);
obj.PatchY = obj.SY.*repmat((Y4.*.7 + sin(T4).*.3).*.46, [1, mn]) + repmat(rows, [80, 1]);
obj.patchHdl = fill(obj.ax, obj.PatchX, obj.PatchY, datas, 'EdgeColor','none');
obj.patchHdl = reshape(obj.patchHdl, sz);
case 'c2rect'
obj.PatchX = obj.SX.*(repmat(X4.*.46, [1, mn]).*repmat(tRatio, [80, 1]) + repmat(cos(T4).*.46, [1, mn]).*repmat(1 - tRatio, [80, 1])) + repmat(cols, [80, 1]);
obj.PatchY = obj.SY.*(repmat(Y4.*.46, [1, mn]).*repmat(tRatio, [80, 1]) + repmat(sin(T4).*.46, [1, mn]).*repmat(1 - tRatio, [80, 1])) + repmat(rows, [80, 1]);
obj.patchHdl = fill(obj.ax, obj.PatchX, obj.PatchY, datas, 'EdgeColor',[1,1,1].*.3, 'LineWidth',.8);
obj.patchHdl = reshape(obj.patchHdl, sz);
case 'shade'
obj.PatchX = obj.SX.*repmat([-.5; .5; .5; -.5].*.98, [1, mn]) + repmat(cols, [4, 1]);
obj.PatchY = obj.SY.*repmat([-.5; -.5; .5; .5].*.98, [1, mn]) + repmat(rows, [4, 1]);
obj.patchHdl = fill(obj.ax, obj.PatchX, obj.PatchY, datas, 'EdgeColor','none');
obj.patchHdl = reshape(obj.patchHdl, sz);
obj.PieX = obj.SX.*repmat([.49; .01; nan; .49; -.49; nan; -.01; -.49; nan], [1, mn]) + repmat(cols, [9, 1]);
obj.PieY = obj.SY.*repmat([-.01; -.49; nan; .49; -.49; nan; .49; .01; nan], [1, mn]) + repmat(rows, [9, 1]);
obj.pieHdl = fill(obj.ax, obj.PieX, obj.PieY, datas, 'EdgeColor',[1,1,1], 'LineWidth',1, 'LineJoin','chamfer', 'EdgeAlpha',.7);
obj.pieHdl = reshape(obj.pieHdl, sz);
case 'pie'
obj.PieX = obj.SX.*repmat(cos(baseT).*.92.*.5, [1, mn]) + repmat(cols, [length(baseT), 1]);
obj.PieY = obj.SY.*repmat(sin(baseT).*.92.*.5, [1, mn]) + repmat(rows, [length(baseT), 1]);
obj.pieHdl = fill(obj.ax, obj.PieX, obj.PieY, [1,1,1], 'EdgeColor',[1,1,1].*.3, 'LineWidth',.8);
obj.pieHdl = reshape(obj.pieHdl, sz);
tMesh = repmat(linspace(0, 1, 100).', 1, mn);
tTheta = pi/2 + tMesh.*repmat(datas./obj.maxV.*2.*pi, 100, 1);
obj.PatchX = obj.SX.*[zeros(1, mn); cos(tTheta).*.92.*.5] + repmat(cols, [101, 1]);
obj.PatchY = obj.SY.*[zeros(1, mn);-sin(tTheta).*.92.*.5] + repmat(rows, [101, 1]);
obj.patchHdl = fill(obj.ax, obj.PatchX, obj.PatchY, datas, 'EdgeColor',[1,1,1].*.3, 'LineWidth',.8);
obj.patchHdl = reshape(obj.patchHdl, sz);
case 'circ'
obj.PatchX = obj.SX.*repmat(cos(baseT).*.92.*.5, [1, mn]) + repmat(cols, [length(baseT), 1]);
obj.PatchY = obj.SY.*repmat(sin(baseT).*.92.*.5, [1, mn]) + repmat(rows, [length(baseT), 1]);
obj.patchHdl = fill(obj.ax, obj.PatchX, obj.PatchY, datas, 'EdgeColor','none', 'LineWidth',.8);
obj.patchHdl = reshape(obj.patchHdl, sz);
case 'acirc'
obj.PatchX = obj.SX.*repmat(cos(baseT).*.92.*.5, [1, mn]).*repmat(tRatio, [length(baseT), 1]) + repmat(cols, [length(baseT), 1]);
obj.PatchY = obj.SY.*repmat(sin(baseT).*.92.*.5, [1, mn]).*repmat(tRatio, [length(baseT), 1]) + repmat(rows, [length(baseT), 1]);
obj.patchHdl = fill(obj.ax, obj.PatchX, obj.PatchY, datas, 'EdgeColor','none', 'LineWidth',.8);
obj.patchHdl = reshape(obj.patchHdl, sz);
case 'arrect'
tRatio2 = max(0, 4*(tRatio - 0.75));
obj.PatchX = obj.SX.*(repmat(X4.*.46, [1, mn]).*repmat(tRatio2, [80, 1]) + repmat(cos(T4).*.46, [1, mn]).*repmat(1 - tRatio2, [80, 1])).*repmat(tRatio, [80, 1]) + repmat(cols, [80, 1]);
obj.PatchY = obj.SY.*(repmat(Y4.*.46, [1, mn]).*repmat(tRatio2, [80, 1]) + repmat(sin(T4).*.46, [1, mn]).*repmat(1 - tRatio2, [80, 1])).*repmat(tRatio, [80, 1]) + repmat(rows, [80, 1]);
obj.patchHdl = fill(obj.ax, obj.PatchX, obj.PatchY, datas, 'EdgeColor',[1,1,1].*.3, 'LineWidth',.8);
obj.patchHdl = reshape(obj.patchHdl, sz);
case 'oval'
tValue = datas./obj.maxV;
baseA = 1 + (tValue <= 0).*tValue;
baseB = 1 - (tValue >= 0).*tValue;
baseOvalX = repmat(cos(baseT).*.98.*.5, [1, mn]).*repmat(baseA, [length(baseT), 1]);
baseOvalY = repmat(sin(baseT).*.98.*.5, [1, mn]).*repmat(baseB, [length(baseT), 1]);
baseOvalXY = [baseOvalX(:), baseOvalY(:)]*thetaMat;
obj.PatchX = obj.SX.*reshape(baseOvalXY(:,1), [length(baseT), mn]) + repmat(cols, [length(baseT), 1]);
obj.PatchY = -obj.SY.*reshape(baseOvalXY(:,2), [length(baseT), mn]) + repmat(rows, [length(baseT), 1]);
obj.patchHdl = fill(obj.ax, obj.PatchX, obj.PatchY, datas, 'EdgeColor',[1,1,1].*.3, 'LineWidth',.8);
obj.patchHdl = reshape(obj.patchHdl, sz);
case 'hex'
obj.PatchX = obj.SX.*repmat(cos(hexT).*.92.*.5, [1, mn]).*repmat(tRatio, [length(hexT), 1]) + repmat(cols, [length(hexT), 1]);
obj.PatchY = obj.SY.*repmat(sin(hexT).*.92.*.5, [1, mn]).*repmat(tRatio, [length(hexT), 1]) + repmat(rows, [length(hexT), 1]);
obj.patchHdl = fill(obj.ax, obj.PatchX, obj.PatchY, datas, 'EdgeColor',[1,1,1].*.3, 'LineWidth',.8);
obj.patchHdl = reshape(obj.patchHdl, sz);
case 'star'
tValue = datas./obj.maxV;
tR = [1;.5;1;.5;1;.5;1;.5;1;.5;1];
obj.PatchX = obj.SX.*repmat(cos(starT).*.92.*.5.*tR, [1, mn]).*repmat(tValue, [length(starT), 1]) + repmat(cols, [length(starT), 1]);
obj.PatchY = -obj.SY.*repmat(sin(starT).*.92.*.5.*tR, [1, mn]).*repmat(tValue, [length(starT), 1]) + repmat(rows, [length(starT), 1]);
obj.patchHdl = fill(obj.ax, obj.PatchX, obj.PatchY, datas, 'EdgeColor',[1,1,1].*.3, 'LineWidth',.8);
obj.patchHdl = reshape(obj.patchHdl, sz);
case 'arrow'
tValue = 2.*((datas < 0) - .5);
obj.PatchX = obj.SX.*repmat(XA.*.8, [1, mn]) + repmat(cols, [7, 1]);
obj.PatchY = obj.SY.*repmat(YA.*.8, [1, mn]).*repmat(tValue, [7, 1]) + repmat(rows, [7, 1]);
obj.patchHdl = fill(obj.ax, obj.PatchX, obj.PatchY, datas, 'EdgeColor',[1,1,1].*.3, 'LineWidth',.8, 'LineJoin','chamfer');
obj.patchHdl = reshape(obj.patchHdl, sz);
case {'tril', 'trill'}
obj.PatchX = obj.SX.*repmat([-.5; .5; -.5].*.98, [1, mn]) + repmat(cols, [3, 1]);
obj.PatchY = obj.SY.*repmat([.5; .5; -.5].*.98, [1, mn]) + repmat(rows, [3, 1]);
obj.patchHdl = fill(obj.ax, obj.PatchX, obj.PatchY, datas, 'EdgeColor','none', 'LineWidth',.8);
obj.patchHdl = reshape(obj.patchHdl, sz);
case {'triu', 'triur'}
obj.PatchX = obj.SX.*repmat([-.5; .5; .5].*.98, [1, mn]) + repmat(cols, [3, 1]);
obj.PatchY = obj.SY.*repmat([-.5; .5; -.5].*.98, [1, mn]) + repmat(rows, [3, 1]);
obj.patchHdl = fill(obj.ax, obj.PatchX, obj.PatchY, datas, 'EdgeColor','none', 'LineWidth',.8);
obj.patchHdl = reshape(obj.patchHdl, sz);
case 'triul'
obj.PatchX = obj.SX.*repmat([.5; -.5; -.5].*.98, [1, mn]) + repmat(cols, [3, 1]);
obj.PatchY = obj.SY.*repmat([-.5; -.5; .5].*.98, [1, mn]) + repmat(rows, [3, 1]);
obj.patchHdl = fill(obj.ax, obj.PatchX, obj.PatchY, datas, 'EdgeColor','none', 'LineWidth',.8);
obj.patchHdl = reshape(obj.patchHdl, sz);
case 'trilr'
obj.PatchX = obj.SX.*repmat([-.5; .5; .5].*.98, [1, mn]) + repmat(cols, [3, 1]);
obj.PatchY = obj.SY.*repmat([.5; .5; -.5].*.98, [1, mn]) + repmat(rows, [3, 1]);
obj.patchHdl = fill(obj.ax, obj.PatchX, obj.PatchY, datas, 'EdgeColor','none', 'LineWidth',.8);
obj.patchHdl = reshape(obj.patchHdl, sz);
case 'donut'
obj.PieX = obj.SX.*repmat([cos(baseT - pi/2).*.92.*.5; cos(baseT(end:-1:1, :) - pi/2).*.92.*.25], [1, mn]) + repmat(cols, [2*length(baseT), 1]);
obj.PieY = obj.SY.*repmat([sin(baseT - pi/2).*.92.*.5; sin(baseT(end:-1:1, :) - pi/2).*.92.*.25], [1, mn]) + repmat(rows, [2*length(baseT), 1]);
obj.pieHdl = fill(obj.ax, obj.PieX, obj.PieY, [1,1,1], 'EdgeColor',[1,1,1].*.3, 'LineWidth',.8);
obj.pieHdl = reshape(obj.pieHdl, sz);
tMesh = repmat(linspace(0, 1, 50).', 1, mn);
tTheta = pi/2 + tMesh.*repmat(datas./obj.maxV.*2.*pi, 50, 1);
obj.PatchX = obj.SX.*[cos(tTheta).*.92.*.5; cos(tTheta(end:-1:1, :)).*.92.*.25] + repmat(cols, [100, 1]);
obj.PatchY = -obj.SY.*[sin(tTheta).*.92.*.5; sin(tTheta(end:-1:1, :)).*.92.*.25] + repmat(rows, [100, 1]);
obj.patchHdl = fill(obj.ax, obj.PatchX, obj.PatchY, datas, 'EdgeColor',[1,1,1].*.3, 'LineWidth',.8, 'LineJoin','chamfer');
obj.patchHdl = reshape(obj.patchHdl, sz);
case 'cust'
obj.PatchX = obj.SX.*repmat(obj.SData(1,:).', [1, mn]) + repmat(cols, [length(obj.SData(1,:)), 1]);
obj.PatchY = obj.SY.*repmat(-obj.SData(2,:).', [1, mn]) + repmat(rows, [length(obj.SData(2,:)), 1]);
obj.patchHdl = fill(obj.ax, obj.PatchX, obj.PatchY, datas, 'EdgeColor',[1,1,1].*.3, 'LineWidth',.8);
obj.patchHdl = reshape(obj.patchHdl, sz);
case 'acust'
obj.PatchX = obj.SX.*repmat(obj.SData(1,:).', [1, mn]).*repmat(tRatio, [length(obj.SData(1,:)), 1]) + repmat(cols, [length(obj.SData(1,:)), 1]);
obj.PatchY = obj.SY.*repmat(-obj.SData(2,:).', [1, mn]).*repmat(tRatio, [length(obj.SData(2,:)), 1]) + repmat(rows, [length(obj.SData(2,:)), 1]);
obj.patchHdl = fill(obj.ax, obj.PatchX, obj.PatchY, datas, 'EdgeColor',[1,1,1].*.3, 'LineWidth',.8);
obj.patchHdl = reshape(obj.patchHdl, sz);
case 'bcirc'
obj.PieX = obj.SX.*repmat(cos(baseT).*.92.*.5, [1, mn]) + repmat(cols, [length(baseT), 1]);
obj.PieY = obj.SY.*repmat(sin(baseT).*.92.*.5, [1, mn]) + repmat(rows, [length(baseT), 1]);
obj.pieHdl = fill(obj.ax, obj.PieX, obj.PieY, [1,1,1], 'EdgeColor',[1,1,1].*.3, 'LineWidth',.8);
obj.pieHdl = reshape(obj.pieHdl, sz);
obj.PatchX = obj.SX.*repmat(cos(baseT).*.92.*.5, [1, mn]).*repmat(tRatio, [length(baseT), 1]) + repmat(cols, [length(baseT), 1]);
obj.PatchY = obj.SY.*repmat(sin(baseT).*.92.*.5, [1, mn]).*repmat(tRatio, [length(baseT), 1]) + repmat(rows, [length(baseT), 1]);
obj.patchHdl = fill(obj.ax, obj.PatchX, obj.PatchY, datas, 'EdgeColor',[1,1,1].*.3, 'LineWidth',.8);
obj.patchHdl = reshape(obj.patchHdl, sz);
end
% obj.textHdl = gobjects(size(obj.Data, 1), size(obj.Data, 2));
% Draw box lines (绘制小框线)
obj.BX = []; obj.BY = [];
for gi = 1:max(obj.RowGroup)
for gj = 1:max(obj.ColGroup)
posi = obj.RP(obj.RowGroup == gi);
posj = obj.CP(obj.ColGroup == gj);
rowY = unique([posi - .5, posi + .5]);
rowX = [posj(1) - .5; posj(end) + .5; nan]*ones(size(rowY));
rowY = [1; 1; nan]*rowY;
colX = unique([posj - .5, posj + .5]);
colY = [posi(1) - .5; posi(end) + .5; nan]*ones(size(colX));
colX = [1; 1; nan]*colX;
obj.BX = [obj.BX; rowX(:); colX(:)];
obj.BY = [obj.BY; rowY(:); colY(:)];
end
end
obj.boxHdl = plot(obj.ax, obj.BX, obj.BY, ...
'LineWidth', 0.8, 'Color', [1, 1, 1] .* 0.85);
if strcmpi(obj.Format, 'sq') || ...
strcmpi(obj.Format, 'rrect') || ...
strcmpi(obj.Format, 'shade') || ...
strcmpi(obj.Format, 'c2rect')
set(obj.boxHdl, 'Visible','off');
end
if strcmpi(obj.Format, 'shade')
[posR, posC] = find(obj.Data >= 0);
if ~isempty(posR)
for i = 1:length(posR)
row = posR(i); col = posC(i);
set(obj.pieHdl(row, col), 'XData', [0;0;0;0;0;0;0;0;0], ...
'YData', [0;0;0;0;0;0;0;0;0], ...
'Visible','off')
end
end
end
[nanR, nanC] = find(isnan(obj.Data));
if ~isempty(nanR)
obj.nanTextHdl = text(obj.ax, obj.CP(nanC), obj.RP(nanR), ...
'×', 'FontName','Times New Roman', 'HorizontalAlignment','center', 'FontSize',20);
tind = sub2ind(sz, nanR, nanC);
if ~isempty(obj.PatchX)
if size(obj.PatchX, 1) < 4
obj.PatchX(4, :) = nan;
obj.PatchY(4, :) = nan;
end
obj.PatchX(:, tind) = nan;
obj.PatchY(:, tind) = nan;
for i = 1:length(nanR)
row = nanR(i); col = nanC(i);
set(obj.patchHdl(row, col), 'XData', [-.5;.5;.5;-.5].*.98 + obj.CP(col), ...
'YData', [-.5,-.5,.5,.5].*.98 + obj.RP(row), ...
'FaceColor', [.8,.8,.8], 'EdgeColor','none');
obj.PatchX(1:4, tind(i)) = [-.5;.5;.5;-.5].*.98 + obj.CP(col);
obj.PatchY(1:4, tind(i)) = [-.5,-.5,.5,.5].*.98 + obj.RP(row);
end
end
if ~isempty(obj.PieX)
obj.PieX(:, tind) = nan;
obj.PieX(:, tind) = nan;
for i = 1:length(nanR)
row = nanR(i); col = nanC(i);
set(obj.pieHdl(row, col), 'XData', [0;0;0;0], ...
'YData', [0;0;0;0], ...
'FaceColor', [0,0,0]);
end
end
end
if strcmpi(obj.Format, 'txt') || strcmpi(obj.Format, 'text')
obj.setText()
end
obj.FX = []; obj.FY = [];
for gi = 1:max(obj.RowGroup)
for gj = 1:max(obj.ColGroup)
posi = obj.RP(obj.RowGroup == gi);
posj = obj.CP(obj.ColGroup == gj);
numY = round(max(posi) - min(posi) + 2);
obj.FX = [obj.FX, [0, ones(1, numY), zeros(1, numY), 1, nan].*(max(posj) - min(posj) + 1) + min(posj) - .5];
obj.FY = [obj.FY, [0, linspace(0, 1, numY), linspace(1, 0, numY), 0, nan].*(max(posi) - min(posi) + 1) + min(posi) - .5];
end
end
obj.frameHdl = plot(obj.ax, obj.FX, obj.FY, 'Color','k', 'LineWidth',1, 'LineJoin','miter', 'Visible','off');
obj.rowTickHdl = plot(obj.ax, nan, nan, 'Color','k', 'LineWidth',1, 'Visible','off');
obj.colTickHdl = plot(obj.ax, nan, nan, 'Color','k', 'LineWidth',1, 'Visible','off');
if isempty(obj.VarName)
% Create default variable names (生成默认变量名)
obj.VarName{length(obj.Data)} = '';
for i = 1:length(obj.Data)
obj.VarName{i} = ['Var-', num2str(i)];
end
tflag = false;
else
tflag = true;
end
% Add row labels ('Visible', 'off') (添加行标签,默认隐藏)
obj.rowLabelHdl = gobjects(1, size(obj.Data, 1));
for row = 1:size(obj.Data, 1)
obj.rowLabelHdl(row) = text(obj.ax, 0.5 - obj.TickLabelOffset, obj.RP(row), ...
obj.VarName{row}, 'HorizontalAlignment','right', ...
'FontName','Times New Roman', 'FontSize',12, 'Visible','off');
end
% Add column labels ('Visible', 'off') (添加列标签,默认隐藏)
obj.colLabelHdl = gobjects(1, size(obj.Data, 2));
for col = 1:size(obj.Data, 2)
obj.colLabelHdl(col) = text(obj.ax, obj.CP(col), obj.RP(end) + .5 + obj.TickLabelOffset, ...
obj.VarName{col}, 'HorizontalAlignment','right', ...
'FontName','Times New Roman', 'FontSize',12, 'Rotation',30, 'Visible','off');
end
if isempty(obj.RowGroupName)
obj.RowGroupName = compose('Group-%d', unique(obj.RowGroup));
end
if isempty(obj.ColGroupName)
obj.ColGroupName = compose('Group-%d', unique(obj.ColGroup));
end
obj.RGLST = zeros(max(obj.RowGroup), 4);
obj.rowGroupLabelHdl = gobjects(1, max(obj.RowGroup));
for row = 1:max(obj.RowGroup)
obj.rowGroupLabelHdl(row) = text(obj.ax, 0.5 - obj.GroupLabelOffset, obj.RGP(row), ...
obj.RowGroupName{row}, 'HorizontalAlignment','center', 'Rotation',90, ...
'FontName','Times New Roman', 'FontSize',15, 'Visible','off');
obj.RGLST(row, :) = [.5, obj.RGP(row), .5 - obj.GroupLabelOffset, obj.RGP(row)];
end
obj.CGLST = zeros(max(obj.ColGroup), 4);
obj.colGroupLabelHdl = gobjects(1, max(obj.ColGroup));
for col = 1:max(obj.ColGroup)
obj.colGroupLabelHdl(col) = text(obj.ax, obj.CGP(col), obj.RP(end) + .5 + obj.GroupLabelOffset, ...
obj.ColGroupName{col}, 'HorizontalAlignment','center', 'Rotation',0, ...
'FontName','Times New Roman', 'FontSize',15, 'Visible','off');
obj.CGLST(col, :) = [obj.CGP(col), obj.RP(end) + .5, obj.CGP(col), obj.RP(end) + .5 + obj.GroupLabelOffset];
end
% Apply 'Type' if not full
if ~strcmp(obj.Type, 'full')
obj.setType(obj.Type);
end
if tflag
obj.setVarName(obj.VarName);
end
if ~isempty(obj.RowName)
obj.setRowName(obj.RowName);
end
if ~isempty(obj.ColName)
obj.setColLabelLocation('bottom')
obj.setColName(obj.ColName);
end
if nargout == 1
varargout = {obj};
end
end
% =========================================================================
% Text decoration (修饰文本)
% =========================================================================
function varargout = setText(obj, varargin)
% obj.setText(varargin) - Show value labels with auto-contrast
% color and hide based on matrix type, and set properties for labels
% (显示数值标签,自动调整对比色,并根据矩阵类型隐藏部分标签,设置标签属性)
%
% obj.setText(___); Set properties for all value labels.
%
% obj.setText(m, n, ___); Set properties for the m‑th row, n-th column value label.
%
% obj.setText([m1, m2, ...], [n1, n2, ...], ___); Set
% properties for value labels by their indices.
%
% obj.setText([m1; m2; ...], [n1, n2, ...], ___);
% obj.setText([m1, m2, ...], [n1; n2; ...], ___);
% Set properties for all combinations when the row-index-vector
% and col-index-vector have different sizes.
%
% obj.setText([m1, m2, ...], [], ___); Set properties for
% the value labels for all columns in the specified rows.
%
% obj.setText([], [n1, n2, ...], ___); Set properties for
% the value labels for all rows in the specified columns.
%
% obj.setText(Bool, ___); Set properties for value labels
% where the logical matrix Bool is true.
if ~obj.TxtShown
[cols, rows] = meshgrid(1:size(obj.Data, 2), 1:size(obj.Data, 1));
rows = obj.RP(rows);
cols = obj.CP(cols);
dataVec = obj.Data(:);
strCell = cell(size(dataVec));
valid = ~isnan(dataVec);
strCell(valid) = cellstr(num2str(dataVec(valid), '%.2f'));
strCell(~valid) = {''};
hAll = text(obj.ax, cols(:), rows(:), strCell, ...
'FontName', 'Times New Roman', ...
'HorizontalAlignment', 'center', ...
'Visible', 'off');
hAll(~valid) = obj.nanTextHdl;
obj.textHdl = reshape(hAll, size(obj.Data));
obj.TxtShown = true;
end
if isempty(varargin)
varargin = {'Visible','on'};
end
if islogical(varargin{1})
[M, N] = find(varargin{1});
for i = 1:length(M)
m = M(i); n = N(i);
set(obj.textHdl(m, n), varargin{2:end})
end
elseif isnumeric(varargin{1})
M = varargin{1}; N = varargin{2};
if all(size(M) == size(N))
for i = 1:length(M)
m = M(i); n = N(i);
set(obj.textHdl(m, n), varargin{3:end})
end
else
if isempty(M); M = 1:size(obj.Data, 1); end
if isempty(N); N = 1:size(obj.Data, 2); end
for i = 1:length(M)
for j = 1:length(N)
m = M(i); n = N(j);
set(obj.textHdl(m, n), varargin{3:end})
end
end
end
else
% Get grayscale of current colormap (获取当前颜色映射的灰度值)
cmp = get(obj.ax, 'Colormap');
graymap = mean(cmp, 2);
climit = get(obj.ax, 'CLim');
values = linspace(climit(1), climit(2), size(cmp, 1) + 1);
% Loop over all cells (遍历所有单元格)
for row = 1:size(obj.Data, 1)
for col = 1:size(obj.Data, 2)
% Determine text color (black if background bright, white if dark)
% (根据背景亮度决定文字颜色:亮底黑字,暗底白字)
if ~isnan(obj.Data(row, col))
if strcmpi(obj.Format, 'txt') || strcmpi(obj.Format, 'text')
% textColor = interp1(linspace(climit(1), climit(2), size(cmp, 1)), ...
% cmp, obj.Data(row, col));
tind = sum(obj.Data(row, col) >= values);
tind(tind <= 0) = 1;
tind(tind > size(cmp, 1)) = size(cmp, 1);
textColor = cmp(tind, :);
else
% bgBrightness = interp1(linspace(climit(1), climit(2), size(graymap, 1)), ...
% graymap, obj.Data(row, col));
tind = sum(obj.Data(row, col) >= values);
tind(tind <= 0) = 1;
tind(tind > size(cmp, 1)) = size(cmp, 1);
bgBrightness = graymap(tind);
if bgBrightness < 0.5
textColor = [1, 1, 1]; % white (白色)
else
textColor = [0, 0, 0]; % black (黑色)
end
end
set(obj.textHdl(row, col), 'Color',textColor)
end
set(obj.textHdl(row, col), 'Visible','on', varargin{:});
end
end
switch lower(obj.Type)
case 'triu' % upper triangle (including diagonal) (上三角,含对角线)
for row = 1:size(obj.Data, 1)
for col = 1:(row - 1)
set(obj.textHdl(row, col), 'Visible','off');
end
end
case 'tril' % lower triangle (including diagonal) (下三角,含对角线)
for col = 1:size(obj.Data, 2)
for row = 1:(col - 1)
set(obj.textHdl(row, col), 'Visible','off');
end
end
case {'triu0', 'linku', 'varu'} % upper triangle without diagonal (扣除对角线,上三角不含对角线)
for row = 1:size(obj.Data, 1)
for col = 1:(row)
set(obj.textHdl(row, col), 'Visible','off');
end
end
case {'tril0', 'linkl', 'varl'} % lower triangle without diagonal (扣除对角线,下三角不含对角线)
for col = 1:size(obj.Data, 2)
for row = 1:(col)
set(obj.textHdl(row, col), 'Visible','off');
end
end
end
end
if nargout == 1
varargout = {obj};
end
end
% =========================================================================
% Set properties for patch handles (设置图形样式)
% =========================================================================
% 设置图形样式
function varargout = setPatch(obj, varargin)
% obj.setPatch(varargin) - Apply properties to all patch objects (and pie backgrounds if applicable)
% (为所有填充图形设置属性,对饼图类型同时设置背景)
%
% obj.setPatch(___); Set properties for all patch objects.
%
% obj.setPatch(m, n, ___); Set properties for the m‑th row, n-th column patch object.
%
% obj.setPatch([m1, m2, ...], [n1, n2, ...], ___); Set
% properties for patch objects by their indices.
%
% obj.setPatch([m1; m2; ...], [n1, n2, ...], ___);
% obj.setPatch([m1, m2, ...], [n1; n2; ...], ___);
% Set properties for all combinations when the row-index-vector
% and col-index-vector have different sizes.
%
% obj.setPatch([m1, m2, ...], [], ___); Set properties for
% the patch objects for all columns in the specified rows.
%
% obj.setPatch([], [n1, n2, ...], ___); Set properties for
% the patch objects for all rows in the specified columns.
%
% obj.setPatch(Bool, ___); Set properties for patch objects
% where the logical matrix Bool is true.
if islogical(varargin{1})
[M, N] = find(varargin{1});
for i = 1:length(M)
m = M(i); n = N(i);
set(obj.patchHdl(m, n), varargin{2:end});
if strcmpi(obj.Format, 'pie') || ...
strcmpi(obj.Format, 'donut') || ...
strcmpi(obj.Format, 'bcirc') || ...
strcmpi(obj.Format, 'shade')
set(obj.pieHdl(m, n), varargin{2:end});
end
end
elseif isnumeric(varargin{1})
M = varargin{1}; N = varargin{2};
if all(size(M) == size(N))
for i = 1:length(M)
m = M(i); n = N(i);
set(obj.patchHdl(m, n), varargin{3:end});
if strcmpi(obj.Format, 'pie') || ...
strcmpi(obj.Format, 'donut') || ...
strcmpi(obj.Format, 'bcirc') || ...
strcmpi(obj.Format, 'shade')
set(obj.pieHdl(m, n), varargin{3:end});
end
end
else
if isempty(M); M = 1:size(obj.Data, 1); end
if isempty(N); N = 1:size(obj.Data, 2); end
for i = 1:length(M)
for j = 1:length(N)
m = M(i); n = N(j);
set(obj.patchHdl(m, n), varargin{3:end});
if strcmpi(obj.Format, 'pie') || ...
strcmpi(obj.Format, 'donut') || ...
strcmpi(obj.Format, 'bcirc') || ...
strcmpi(obj.Format, 'shade')
set(obj.pieHdl(m, n), varargin{3:end});
end
end
end
end
else
for row = 1:size(obj.Data, 1)
for col = 1:size(obj.Data, 2)
if ~isnan(obj.Data(row, col))
set(obj.patchHdl(row, col), varargin{:});
% For pie/donut/bcirc formats, also set the pie background handle
% (对于 pie/donut/bcirc 格式,同时设置饼图背景句柄)
if strcmpi(obj.Format, 'pie') || ...
strcmpi(obj.Format, 'donut') || ...
strcmpi(obj.Format, 'bcirc') || ...
strcmpi(obj.Format, 'shade')
set(obj.pieHdl(row, col), varargin{:});
end
end
end
end
end
if nargout == 1
varargout = {obj};
end
end
% =========================================================================
% Set properties for grid handle (设置网格样式)
% =========================================================================
function varargout = setGrid(obj, varargin)
% obj.setGrid(varargin) - Set properties for grid handle (设置网格样式)
obj.GX = []; obj.GY = [];
for gi = 1:max(obj.RowGroup)
for gj = 1:max(obj.ColGroup)
posi = obj.RP(obj.RowGroup == gi);
posj = obj.CP(obj.ColGroup == gj);
M = length(posi);
N = length(posj);
switch lower(obj.Type)
case {'full', 'row', 'col'}
posi = obj.RP(obj.RowGroup == gi);
posj = obj.CP(obj.ColGroup == gj);
rowY = posi;
rowX = [posj(1); posj(end); nan]*ones(size(rowY));
rowY = [1; 1; nan]*rowY;
colX = posj;
colY = [posi(1); posi(end); nan]*ones(size(colX));
colX = [1; 1; nan]*colX;
obj.GX = [obj.GX; rowX(:); colX(:)];
obj.GY = [obj.GY; rowY(:); colY(:)];
case {'triu0', 'linku', 'varu'}
if gi == gj && length(posi) > 1
gX1 = [1; 1; nan]*posj(2:end);
gY1 = [posi(1).*ones(1, N - 1);
posi(1:(end - 1));
nan(1, N - 1)];
gX2 = [posj(end).*ones(1, N - 1);
posj(2:end);
nan(1, N - 1)];
gY2 = [1; 1; nan]*posj(1:(end - 1));
obj.GX = [obj.GX; gX1(:); gX2(:)];
obj.GY = [obj.GY; gY1(:); gY2(:)];
elseif gj > gi
gX1 = [1; 1; nan]*posj;