-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnplot.m
1152 lines (912 loc) · 43.2 KB
/
nplot.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
classdef nplot < handle
% NPLOT plots a line profile from one or more multidimensional matrices.
%
% NPLOT(mat)
% opens a figure that can plot data along all dimensions of mat.
%
% NPLOT(mat1, mat2, ...)
% opens a figure that can simultaneously plot data along all dimensions of
% the input matrices mat1, mat2, ...
%
%NAME-VALUE PAIRS
%
% NAME TYPE DEFAULT DESCRIPTION
%
% LineStyle string [] Allows to specify the line
% apperance in the same way it is
% possible with matlabs plot
% function.
% DimLabel {char} [] cell array containing character
% vectors the describe the
% dimensions of the data and will
% be shown next to the sliders.
% InputNames {char} [] cell array containing character
% vectors that describe the
% matrices and are shown in the
% plot-legend.
% DimVal {double} Cell array that contains a
% 1D-double array for each
% dimension in the input data
% which will be used for the
% x-axes. Default values are
% linear indices counting from 1
% to the dimension size. Leave
% cell empty for default values.
% -------------------------------------------------------------------------
% TODO
% -------------------------------------------------------------------------
% - show value at indicator position
% - change the way of mouse-navigation:
% left-click, change center for x and y axis
% right-click, change width for x- and y-axis
% -------------------------------------------------------------------------
% Check name-value pairs and set initial values
% -------------------------------------------------------------------------
properties (Access = public)
fig
end
properties (Access = private)
nMats % number of input matrices
nSlider % number of non-singleton dimensions
nDims % number of dimensions
ston % cell array for dimensions where a matrix is the only singleton
valNames % names of input matrices
mat % cell array containing the input matrices
isComplex % is one of the inputs complex
inputNames % names of the input matrices, shown in legend
varargin % all input data is stored in varargin
p % input parser
S % size of input matrices
unit % physical unit of the input data
minmax
%minVal % minVal of all inputData
%maxVal % maxVal of all inputData
%% DISPLAYING
% showDim specifies the dimension that is currently displayed on
% the xaxis
showDim
% stores the current complex representation mode
complexMode
% cell array that stores the location information in the input data
% of each currently shown line profile.
sel
% index of the currently active dimension for slider scrolling
% etc...
activeDim
% label for each dimension
dimLabel
% values for each dimension
dimVal
% apperance of plotted lines
lineStyle
% store currently shown x-data
currXData
% store currently shown y-data
currYData
%% GUI elements
sliderPanelHeight % absolute height of slider panel
controlPanelWidth % width of control panel Ü(normalized)
% panels
pPlot
pSlider
pControl
% slider handles
hSliderLabel
hSlider
hIndex
% locVal handle
hLocAndVals
% button handles
hBtnCmp
% plot elements
hAxis
hVertLine
hPlot
hLine
xaxes
%% external
bCalledFromExternal
bUpdateCaller
hBtnToggleUpdateCaller
end
properties (Constant = true, Hidden = true)
% max number of letters for variable names in the locVal section
maxLetters = 6;
end
events
selChanged
end
methods
function obj = nplot(varargin)
% CONSTRUCTOR
obj.varargin = varargin;
obj.checkInputMatrices();
% The slider for which dimension is initially active?
obj.activeDim = 1;
% set the correct default value for complexMode
if any(obj.isComplex)
obj.complexMode = 1;
else
% if neither input is complex, display the real part of the
% data
obj.complexMode = 3;
end
% per default, update the caller about selection changes
obj.bUpdateCaller = 1;
% prepare the input parser
obj.parseNVPs();
obj.prepareXaxes();
obj.initializeGUI();
% collect/generate names for input matrices
for idm = 1:obj.nMats
obj.inputNames{idm} = inputname(idm);
end
obj.setValNames();
% set initial plot-dimension
obj.changeDimension(obj.hSliderLabel(1))
% -------------------------------------------------------------------------
% Last step, make figure visible
% -------------------------------------------------------------------------
obj.refreshUI()
set(obj.fig, 'Visible', 'on');
% do not assign to 'ans' when called without assigned variable
if nargout == 0
clear obj
end
end
function delete(obj)
% DESTRUCTOR
delete(obj)
end
function checkInputMatrices(obj)
% how many matrices are in varargin?
obj.nMats = find(cellfun(@ischar, obj.varargin), 1, 'first') - 1;
if isempty(obj.nMats)
% no name value pair was provided
obj.nMats = numel(obj.varargin);
end
% define mats in order to suppress warnings
obj.mat = cell(1, obj.nMats);
obj.isComplex = zeros(obj.nMats, 1);
% create a fixed loop counter
N = obj.nMats;
fixidx = 1;
for idx = 1:N
% check if this matrix actually has data
if isempty(obj.varargin{1})
obj.nMats = obj.nMats - 1;
warning('input matrix no. %d is empty.', idx);
else
obj.isComplex(fixidx) = ~isreal(obj.varargin{1});
obj.mat{fixidx} = obj.varargin{1};
fixidx = fixidx + 1;
end
% at the same time, remove matrices from varargin to save space
obj.varargin(1) = [];
end
% get min max values for all input matrices and complex modes
obj.calculateMinMaxAll();
obj.checkSize()
% number of Sliders
obj.nSlider = numel(obj.S);
% number of dimensions
obj.nDims = numel(obj.S);
end
function checkSize(obj)
s = cellfun(@size, obj.mat, 'UniformOutput', false);
maxDim = max(cellfun(@numel, s));
for iMat = 1:obj.nMats
if numel(s{iMat}) < maxDim
s{iMat} = [s{iMat} ones(1, maxDim - numel(s{iMat}))];
end
end
% stack dimension sizes
s_st = cat(1, s{:});
% make sure the are at most 2 different numbers per column
% and one must be equal to 1.
for iCol = 1:maxDim
u = unique(s_st(:, iCol));
if numel(u) > 2 || numel(u) == 2 && u(1) ~= 1
% there are dimensions where the size differs and
% neither of the matrices has a size of one along that
% dimension
error('Matrix dimensions must agree.')
end
end
obj.S = max(s_st, [] , 1);
for iMat = 1:iMat
obj.ston{iMat} = find(s{iMat} == 1);
end
end
function parseNVPs(obj)
obj.p = inputParser;
% prepare default selector
obj.sel = num2cell(ceil(obj.S/2));
addParameter(obj.p, 'InputNames', {}, @(x) ischar(x) || (iscell(x) && numel(x) == obj.nMats));
addParameter(obj.p, 'LineStyle', repmat({'-'}, 1, obj.nMats), @(x) ischar(x) || (iscell(x) && numel(x) == obj.nMats));
addParameter(obj.p, 'InitSel', obj.sel, @(x) isnumeric(x) && x <= 4);
addParameter(obj.p, 'ComplexMode', obj.complexMode, @(x) isnumeric(x) && x <= 4);
addParameter(obj.p, 'DimLabel', strcat(repmat({}, 1, numel(obj.S))), @(x) iscell(x) && numel(x) >= obj.nDims);
addParameter(obj.p, 'DimVal', cellfun(@(x) 1:x, num2cell(obj.S), 'UniformOutput', false), @iscell);
addParameter(obj.p, 'Unit', '', @(x) ischar(x) || iscell(x));
addParameter(obj.p, 'InitDim', 1, @isnumeric);
addParameter(obj.p, 'ExternalCall', 0, @(x) (x == 0 || x == 1));
parse(obj.p, obj.varargin{:});
obj.complexMode = obj.p.Results.ComplexMode;
obj.inputNames = obj.p.Results.InputNames;
obj.lineStyle = obj.p.Results.LineStyle;
obj.unit = obj.p.Results.Unit;
obj.showDim = obj.p.Results.InitDim;
obj.bCalledFromExternal = obj.p.Results.ExternalCall;
obj.parseDimLabelsVals()
end
function initializeGUI(obj)
% -------------------------------------------------------------------------
% Create Figure and partition into panels
% -------------------------------------------------------------------------
fWidth0 = 300;
fHeight0 = 200;
fWidth = 1000;
fHeight = 800;
obj.sliderPanelHeight = (obj.nSlider+1) * 30;%px
obj.controlPanelWidth = 0.2;
obj.fig = figure( ...
'Position', [fWidth0 fHeight0 fWidth fHeight], ...
'Units', 'pixel', ...
'ResizeFcn', @obj.guiResize, ...
'Visible', 'off', ...
'WindowScrollWheelFcn', @obj.scrollSlider, ...
'CloseRequestFcn', @obj.closeRqst);
% -------------------------------------------------------------------------
% define the plot panel and slider panel.
% -------------------------------------------------------------------------
lDiv = 0.2;
obj.pPlot = uipanel( ...
'Units', 'pixels', ...
'Position', [0 obj.sliderPanelHeight fWidth fHeight-obj.sliderPanelHeight]);
obj.pSlider = uipanel( ...
'Units', 'pixels', ...
'Position', [lDiv*fWidth 0 (1-lDiv)*fWidth obj.sliderPanelHeight]);
obj.pControl = uipanel( ...
'Units', 'pixels', ...
'Position', [0 0 lDiv*fWidth obj.sliderPanelHeight]);
% -------------------------------------------------------------------------
% initialize elements in pSlider
% -------------------------------------------------------------------------
% divison of Slider panel width (sum must equal 1) into spaces for
% [Label Index Slider]
division = [0.15 0.1 0.75];
% top and bottom padding in normalized units
tb_pad = 0.05;
% reserve area at the bottom for display of current data value
sliderHeight = (1-2*tb_pad)/(obj.nSlider+1);
for iSlider = 1:obj.nSlider
obj.hSliderLabel(iSlider) = uicontrol( ...
'Parent', obj.pSlider, ...
'Style', 'togglebutton', ...
'Units', 'normalized', ...
'Position', [0 ...
1-tb_pad-iSlider*sliderHeight ...
division(1) ...
sliderHeight], ...
'String', obj.dimLabel{iSlider}, ...
'FontUnits', 'normalized', ...
'FontSize', 0.8, ...
'Callback', {@obj.changeDimension});
obj.hIndex(iSlider) = uicontrol( ...
'Parent', obj.pSlider, ...
'Style', 'edit', ...
'Units', 'normalized', ...
'Position', [division(1) ...
1-tb_pad-iSlider*sliderHeight ...
division(2) ...
sliderHeight], ...
'String', obj.dimVal{iSlider}{obj.sel{iSlider}}, ...
'FontUnits', 'normalized', ...
'FontSize', 0.8, ...
'Enable', 'on');
set(obj.hIndex(iSlider), 'Callback', { @obj.editIndex});
if obj.S(iSlider) == 1
sliderStep = [0, 0];
else
sliderStep = [1/(obj.S(iSlider)-1) 10/(obj.S(iSlider)-1)];
end
obj.hSlider(iSlider) = uicontrol( ...
'Parent', obj.pSlider, ...
'Style', 'slider', ...
'Units', 'normalized', ...
'Position', [sum(division(1:2)) ...
1-tb_pad-iSlider*sliderHeight ...
division(3) ...
sliderHeight], ...
'Min', 1, ...
'Max', obj.S(iSlider), ...
'Value', obj.sel{iSlider}, ...
'SliderStep', sliderStep, ...
'Callback', @obj.sliderMove);
addlistener(obj.hSlider(iSlider), ...
'ContinuousValueChange', @obj.sliderMove);
end
% add loc and val information in last row
obj.hLocAndVals = uicontrol(...
'Parent', obj.pSlider, ...
'Style', 'text', ...
'Units', 'normalized', ...
'Position', [0 ...
1-tb_pad-(iSlider+1)*sliderHeight-tb_pad/2 ...
1 ...
sliderHeight], ...
'String', '', ...
'HorizontalAlignment', 'left', ...
'FontUnits', 'normalized', ...
'FontSize', 0.8);
% -------------------------------------------------------------------------
% initialize elements in control panel
% -------------------------------------------------------------------------
if sum(obj.isComplex > 0)
obj.hBtnCmp = gobjects(4,1);
obj.hBtnCmp(1) = uicontrol( ...
'Parent', obj.pControl, ...
'Style', 'togglebutton', ...
'Units', 'normalized',...
'Position', [0 2/3 0.5 1/3], ...
'String', 'Magnitude', ...
'Callback', {@obj.toggleComplex},...
'FontUnits', 'normalized', ...
'Value', 1, ...
'FontSize', 0.3);
obj.hBtnCmp(2) = uicontrol( ...
'Parent', obj.pControl, ...
'Style', 'togglebutton', ...
'Units', 'normalized',...
'Position', [0.5 2/3 0.5 1/3], ...
'String', 'Phase', ...
'Callback', {@obj.toggleComplex},...
'FontUnits', 'normalized', ...
'Value', 0, ...
'FontSize', 0.3);
obj.hBtnCmp(3) = uicontrol( ...
'Parent', obj.pControl, ...
'Style', 'togglebutton', ...
'Units', 'normalized',...
'Position', [0 1/3 0.5 1/3], ...
'String', 'real', ...
'Callback', {@obj.toggleComplex},...
'FontUnits', 'normalized', ...
'Value', 0, ...
'FontSize', 0.3);
obj.hBtnCmp(4) = uicontrol( ...
'Parent', obj.pControl, ...
'Style', 'togglebutton', ...
'Units', 'normalized',...
'Position', [0.5 1/3 0.5 1/3], ...
'String', 'imaginary', ...
'Callback', {@obj.toggleComplex},...
'FontUnits', 'normalized', ...
'Value', 0, ...
'FontSize', 0.3);
obj.complexMode = 1;
end
if obj.bCalledFromExternal
nLowerRow = 3;
else
nLowerRow = 2;
end
% autoscale button
uicontrol( ...
'Parent', obj.pControl, ...
'Style', 'togglebutton', ...
'Units', 'normalized',...
'Position', [0 0 1/nLowerRow 1/3], ...
'String', 'Autoscale', ...
'Callback', {@obj.autoscale},...
'FontUnits', 'normalized', ...
'Value', 0, ...
'FontSize', 0.3);
% global scale button
uicontrol( ...
'Parent', obj.pControl, ...
'Style', 'togglebutton', ...
'Units', 'normalized',...
'Position', [1/nLowerRow 0 1/nLowerRow 1/3], ...
'String', 'Globalscale', ...
'Callback', {@obj.globalscale},...
'FontUnits', 'normalized', ...
'Value', 0, ...
'FontSize', 0.3);
if obj.bCalledFromExternal
% updateCaller (only if caller exists)
obj.hBtnToggleUpdateCaller = uicontrol( ...
'Parent', obj.pControl, ...
'Style', 'togglebutton', ...
'Units', 'normalized',...
'Position', [2/3 0 1/3 1/3], ...
'String', 'Update: on', ...
'Callback', {@obj.toggleUpdateCaller},...
'FontUnits', 'normalized', ...
'Value', 0, ...
'FontSize', 0.3);
end
% -------------------------------------------------------------------------
% pPlot Elements
% INITIALIZE ELEMENTS OF pPlot PANEL HERE
% -------------------------------------------------------------------------
% scale axis differently when units are provided.
obj.hAxis = axes( ...
'Parent', obj.pPlot, ...
'Units', 'normal', ...
'Position', [0.05 0.05 0.9 0.9]);
%set(obj.hAxis, 'ButtonDownFcn', @startDragFcn)
if ~strcmp(obj.unit, '')
ylabel(obj.unit)
% leave some space on the left for the ylabel
set(obj.hAxis, 'Position', [0.075 0.05 0.9 0.9]);
end
hold on
xData = [1 2];
yData = [-1 1];
obj.hPlot = gobjects(1, obj.nMats);
for iMat = 1:obj.nMats
obj.hPlot(iMat) = plot(xData, yData, obj.lineStyle{iMat});
end
%obj.hVertLine = plot([1 1], [-1 1]);
obj.hVertLine = xline(1);
% Limits = getLimits();
% add a legend
if ~sum(contains('InputNames', obj.p.UsingDefaults))
legend(obj.inputNames)
end
hold off
end
function calculateMinMaxAll(obj)
for ii = 1:obj.nMats
obj.calculateMinMax(ii)
end
end
function calculateMinMax(obj, idx)
% real part
obj.minmax{3}(idx, :) = [min(real(obj.mat{idx}), [], 'all') max(real(obj.mat{idx}), [], 'all')];
if obj.isComplex(idx)
% complex modes only when necessary
obj.minmax{1}(idx, :) = [min(abs(obj.mat{idx}), [], 'all') max(abs(obj.mat{idx}), [], 'all')];
obj.minmax{2}(idx, :) = [min(angle(obj.mat{idx}), [], 'all') max(angle(obj.mat{idx}), [], 'all')];
obj.minmax{4}(idx, :) = [min(imag(obj.mat{idx}), [], 'all') max(imag(obj.mat{idx}), [], 'all')];
end
end
function minmax = getGlobalMinMax(obj)
% returns global [min max] value for current complex mode and
% all displayed matrices.
minmax = [min(obj.minmax{obj.complexMode}(:, 1), [], 'all') max(obj.minmax{obj.complexMode}(:, 2), [], 'all')];
end
function prepareXaxes(obj)
% find axis with values that cannot be converted to numbers
for ii = 1:numel(obj.dimVal)
obj.xaxes{ii}.ticklabels = obj.dimVal{ii};
if numel(cell2mat(cellfun(@str2num, obj.dimVal{ii}, 'UniformOutput', false))) ~= numel(obj.dimVal{ii})
% some entries in dimVal for this axis cannot be
% converted to numbers
obj.xaxes{ii}.tickvalues = 1:numel(obj.dimVal{ii});
else
% all enries in dimVal can be converted to numbers
obj.xaxes{ii}.tickvalues = cellfun(@str2num, obj.dimVal{ii});
end
end
end
function refreshUI(obj)
% refreshUI()
%
% called by: scrollSlider, sliderMove, editIndex, toggleComplex
% check slider values and update slider UI elements
obj.checkAndRefreshSliders();
% create temporary selector
tempSel = obj.sel;
% clear yData cache
obj.currYData = zeros(obj.nMats, obj.S(obj.showDim));
% replot data from all inputs
for idm = 1:obj.nMats
% create temporary selector
tempSel = obj.sel;
% set default line style
LineStyle = '-';
if isempty(obj.ston{idm})
tempSel{obj.showDim} = ':';
LineStyle = '-';
else
% use default line style if all ston dimensions are 1
% in sel
selAtSton = cell2mat(tempSel(obj.ston{idm}));
if sum(selAtSton) ~= numel(selAtSton)
LineStyle = '--';
end
tempSel(obj.ston{idm}) = {1};
tempSel{obj.showDim} = ':';
end
obj.currYData(idm, :) = obj.complexPart(obj.mat{idm}(tempSel{:}));
set(obj.hPlot(idm), ...
'YData', obj.currYData(idm, :), ...
'XData', obj.xaxes{obj.showDim}.tickvalues, ...
'LineStyle', LineStyle);
end
% reposition vertical line
set(obj.hVertLine, 'Value', obj.xaxes{obj.showDim}.tickvalues(obj.sel{obj.showDim}))
% inform the calling object, that the selector has changed
if obj.bUpdateCaller
notify(obj, 'selChanged')
end
% update the locVal string
obj.locVal()
end
function checkAndRefreshSliders(obj)
% called by: refreshUI
for iSldr = 1:obj.nSlider
% check whether slider still in bounds
obj.sel{iSldr} = max([obj.sel{iSldr}, 1]);
obj.sel{iSldr} = min([obj.sel{iSldr}, obj.S(iSldr)]);
obj.sel{iSldr} = round(obj.sel{iSldr});
%set(hIndex(iSldr), 'String', num2str(sliderVals(iSldr)));
set(obj.hSlider(iSldr), 'Value', obj.sel{iSldr});
set(obj.hIndex(iSldr), 'String', obj.xaxes{iSldr}.ticklabels{obj.sel{iSldr}})
end
end
function setValNames(obj)
for ii = 1:obj.nMats
% create default val name
obj.valNames{ii} = ['val' num2str(ii)];
% if available, take the name of the input variable
if ~isempty(obj.inputNames{ii})
if numel(obj.inputNames{ii}) > obj.maxLetters
obj.valNames{ii} = obj.inputNames{ii}(1:obj.maxLetters);
else
obj.valNames{ii} = obj.inputNames{ii};
end
% text is shown using the LaTeX interpreter. We need to
% escape underscores
obj.valNames{ii} = strrep(obj.valNames{ii}, '_', '\_');
end
end
% to get the number of the displayed characters correct, we
% need to know howm many '_' are in each name to compensate the
% result from numel
usNo = cellfun(@(x) sum(x == '_'), obj.valNames);
% find number of trailing whitespace
wsToAdd = max(cellfun(@numel, obj.valNames) - usNo) - (cellfun(@numel, obj.valNames) - usNo);
ws = cell(1, obj.nMats);
for ii = 1:obj.nMats
ws{ii} = repmat(' ', [1, wsToAdd(ii)]);
end
obj.valNames = strcat(obj.valNames, ws);
end
function parseDimLabelsVals(obj)
%% dimension labels
% set default values for dimLabel
obj.dimLabel = strcat(repmat({'Dim'}, 1, numel(obj.S)), cellfun(@num2str, num2cell(1:obj.nDims), 'UniformOutput', false));
if ~contains('DimLabel', obj.p.UsingDefaults)
% check number of input labels equals dimensions of image
if numel(obj.p.Results.DimLabel) ~= obj.nDims
warning('Number of DimLabel is not equal to the number of image dimensions.')
end
% if cell entry is empty, use default value
emptyCell = cellfun(@isempty, obj.p.Results.DimLabel);
obj.dimLabel(~emptyCell) = obj.p.Results.DimLabel(~emptyCell);
end
%% dimension values
% set default values via size of each dimension
obj.dimVal = cellfun(@(x) 1:x, num2cell(obj.S), 'UniformOutput', false);
if ~contains('DimVal', obj.p.UsingDefaults)
% check number of value arrays equals dimensions of image
if numel(obj.p.Results.DimVal) ~= obj.nDims
% allow for trailing singleton dimensions
% check for all surplus dimensions in DimVal, that
% their size is one.
if numel(obj.p.Results.DimVal) > obj.nDims && ~all(cellfun(@numel, obj.p.Results.DimVal(obj.nDims+1:end)))
error('Number of elements in DimVal must equal the number of image dimensions.')
end
end
% if cell entry is empty, use default value
emptyCell = cellfun(@isempty, obj.p.Results.DimVal);
obj.dimVal(~emptyCell) = obj.p.Results.DimVal(~emptyCell);
% value array for each dimension must have obj.S entries
% if ~isequal(obj.S, cellfun(@numel, obj.dimVal))
% error('Number of elements in DimVal for dimension(s) %s do not match image size', mat2str(find(obj.S ~= cellfun(@numel, obj.dimVal))))
% end
end
obj.dimVal = valsToString(obj.dimVal);
end
function out = complexPart(obj, in)
% complexPart(in)
% in: array with (potentially) complex data
% out: magnitude, phase, real or imaginary part of in
%
% called by: refreshUI
%
% depending on the value in 'complexMode' either the magnitude,
% phase, real part or imaginary part is returned
switch(obj.complexMode)
case 1
out = abs(in);
case 2
out = angle(in);
case 3
out = real(in);
case 4
out = imag(in);
end
end
%% functions to control object from external
function setSelector(obj, newSel)
% setSelector(newSel)
% newSel: cell array containing all new selector values
%
% Called by: external
%
% Is called from external, when the display of different data
% is requested.
% perform some input checks
if numel(newSel) ~= numel(obj.sel)
error('Size mismatch in data selector')
end
obj.sel = newSel;
obj.refreshUI();
end
function out = getSelector(obj)
% getSelector(newSel)
%
% Called by: external
%
% Is called from external, to get the current selector
out = obj.sel;
end
function setDimension(obj, newDim)
% source: index of the new dimension shown along ax-axis
%
% Called by: external or button callback
%
% Is called from external or button callback, to change the
% dimension of the input matrices shown along the x-axis.
% perform some input checks
if newDim > obj.nDims
error('Dimension value exceeds number of dimensions')
end
obj.showDim = newDim;
% change x-axis values
set(obj.hPlot, 'XData', obj.xaxes{obj.showDim}.tickvalues(:))
% store x-data cache
obj.currXData = obj.xaxes{obj.showDim}.tickvalues(:);
% set axis limits automatically
xlim(obj.hAxis, 'auto')
% unpress all buttons
set(obj.hSliderLabel, 'Value', 0);
% press current button
set(obj.hSliderLabel(obj.showDim), 'Value', 1);
obj.refreshUI()
end
function setVisibility(obj, layerShown)
% setVisibility(layerShown)
% newSel: array containing logical values for visibility
% of each plot
%
% Called by: external
%
% Is called from external, to update the visibility of
% individual plots
for iMat = 1:obj.nMats
set(obj.hPlot(iMat), 'Visible', layerShown(iMat))
end
end
function locVal(obj)
% called by: refreshUI
%
% everytime the UI is refreshed, the string containing the
% current point and value is refreshed too.
str = [];
for idm = 1:obj.nMats
if idm~=1
str = [str ' '];
end
sel = obj.sel;
if ~isempty(obj.ston{idm})
sel(obj.ston{idm}) = {1};
end
val = obj.complexPart(obj.mat{idm}(sel{:}));
str = [str sprintf('%s = %f', obj.valNames{idm}, val)];
end
set(obj.hLocAndVals, 'String', str);
end
%% functions for button callbacks
function toggleComplex(obj, source, ~)
% toggleComplex(source, ~)
% source: handle to uicontrol button
%
% called by: uicontrol togglebutton: Callback
%
% Is called when one of the 4 complex data buttons is pressed.
% These buttons are only visible when at least one matrix has
% complex data.
% Depending on which button was pressed last, the magnitude, phase,
% real part or imaginary part of complex data is shown.
% set all buttons unpreessed
set(obj.hBtnCmp, 'Value', 0)
% find the index of the pressed button
btnIdx = find(source == obj.hBtnCmp);
obj.complexMode = btnIdx;
set(obj.hBtnCmp(btnIdx), 'Value', 1)
obj.refreshUI()
end
function autoscale(obj, ~, ~)
% autoscale(~, ~)
%
% called by: autoscale button
%
% set the x- and y-limits such that data fills the axis entirely
set(obj.hAxis, 'XLim', [min(obj.currXData) max(obj.currXData)]);
% in case of a constant line
if min(obj.currYData, [], 'all') == max(obj.currYData, [], 'all')
set(obj.hAxis, 'YLim', [min(obj.currYData, [], 'all')-1 min(obj.currYData, [], 'all')+1]);
else
set(obj.hAxis, 'YLim', [min(obj.currYData, [], 'all') max(obj.currYData, [], 'all')]);
end
end
function globalscale(obj, ~, ~)
% globalscale(~, ~)
%
% called by: globalscale button
%
% set the x- and y-limits to the min and max values of all
% shown input data
% x limits
set(obj.hAxis, 'XLim', [min(obj.currXData) max(obj.currXData)]);
% y limits
minAndMax = obj.getGlobalMinMax();
% in case of a constant line
if minAndMax(1) == minAndMax(2)
set(obj.hAxis, 'YLim', minAndMax + [-1 1]);
else
set(obj.hAxis, 'YLim', minAndMax);
end
end
function toggleUpdateCaller(obj, ~, ~)
% toggleUpdateCaller(~, ~)
%
% called by: UpdateCaller button
%
% toggle the value of bUpdateCaller
if obj.bUpdateCaller == 0
obj.bUpdateCaller = 1;
set(obj.hBtnToggleUpdateCaller , 'String', 'Update: on')
notify(obj, 'selChanged')
elseif obj.bUpdateCaller == 1
obj.bUpdateCaller = 0;
set(obj.hBtnToggleUpdateCaller , 'String', 'Update: off')
end
end
%% functions controlling slider behaviour
function scrollSlider(obj, ~, evtData)
% scrollSlider(source, evtData)