-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathplotECG.m
More file actions
1434 lines (1200 loc) · 54.9 KB
/
plotECG.m
File metadata and controls
1434 lines (1200 loc) · 54.9 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 [ varargout ] = plotECG( varargin )
% -------------------------------------------------------
%
% plotECG( X,Y, varargin ) - Plot Very Long, Multichannel Signals - Zoom & Scroll via Slider
%
% Enables you to plot and zoom & scroll through signals with millions of samples.
% There is one slider for scrolling and one for zooming.
% To move forward or backward by exactly one screen width,
% use the scroll wheel or click on the slider trough.
% Clicking on the arrow buttons moves the signal by 1/100
% the screen width. Scrolling by mouse wheel works only if
% no interactive mode (Datacursor, Zoom, Pan, Edit Plot, etc.) is active.
%
% If you launch plotECG() without any arguments,
% one of two demos is shown.
%
% Ver. 1.1.1
%
% Created: Daniel Frisch (15.02.2015)
% Last modified: Daniel Frisch (09.01.2017)
%
%
% -------------------------------------------------------
%
%
% Input Arguments
% X Vector of timesteps, or scalar sample rate. scalar or [N x 1] double
% Y Signal vector, or signal matrix where each column represents one signal. [N x m] double
% LineSpec LineSpec string, max. 4 characters, see Matlab plot() documentation. string (optional, default: '.')
% If you define a marker here, set 'AutoMarkers' to 'none' so it won't be overridden.
%
% Key-Value Parameters (optional):
% Filter Change parameters like cutoff frequencies via slider or text edit and see the results on the fly.
% Filter function handle for Y. function_handle or string (default: 'none')
% Use one of the builtin filter functions as string, 'filter_FFT', or 'filter_bandpass_notch',
% or define an own filter function and pass its name as string or function_handle (see below).
% mmPerSec Initial zoom in screen millimeters per second. scalar double (default: 50)
% If you change the figure size in this mode, the axis XLim will change such that mmPerSec stays the same.
% secPerScreenWidth Initial zoom setting in seconds that are displayed on screen at a time. scalar double
% If you change the figure size in this mode, the signal scale will change too, such that XLim stays the same.
% ShowAxisTicks Shows the axis ticks and labels and a grid. string, 'on' or 'off' (default: 'on')
% You can also change axis properties on the returned handles: hs.ax.XLabel.String = 'Seconds';
% ShowInformationList Shows information about the location of the last clicks in the signal. function_handle or string
% Specify one of 'none' (default), 'std_InformationList', 'ecg_InformationList', as string,
% or define an own function like those with two inputs and cell output and pass its name or function_handle.
% Mouse clicks are captured only if no interactive mode (pan, zoom etc.) is active in the figure.
% AutoStackSignals Cell array of strings with signal names. Length must be equal to number of columns of Y, or 0. (default: {})
% Stacks the signals vertically, so for example a multipolar ECG can be shown.
% Example: {'I','II','III', 'aVR','aVL','aVF', 'V1','V2','V3','V4','V5','V6'}
% SecondXAxisFunction Function handle that maps from the provided X values to a different x axis scale
% that will be displayed above the plotted signal. function_handle or string (default: 'none')
% Example: @(x)x/60^2, shows the time also in hours.
% SecondXAxisLabel Label of second x axis. Example: 'Time in h'. string (default: '')
% YLimMode 'dynamic': dynamic y axis limits according to minimum and maximum of currently visible signal (default)
% 'fixed' : fixed y axis limits according to minimum and maximum of entire signal.
% In 'fixed' mode, you shouldn't apply filters that change the signal's mean much because YLim won't be updated.
% You can change the fixed interval afterwards using the returned axes handle: hs.ax.YLim = [-10,10];
% AutoMarkers Automatically shows the specified marker at the sample locations if you zoom in such that there are
% more than 5 pixes per sample. Use 'none' to disable this behaviour. string (default: '.')
% ColorOrder Custom ColorOrder for axes properties. [N x 3] double
% ScrollCallback Function handle that will be called when the signal is scrolled or zoomed. function_handle or string (default: 'none')
% An information structure will be passed to the function, with the fields
% - XLim (containing the left and right x-axis value)
% - ContinuousValueChange (whether it was called during or at the end of sliding)
% To keep the sliding smooth, you should not execute expensive code if ContinuousValueChange is true.
% Parent Parent figure. (default: new figure is created)
% Use delete(findall(hs.panel)) or close the figure to delete the old plot and its signals.
% The 'HandleVisibility' of the figure created by default is set to 'Callback' to prevent you from
% plotting into it accidentially from the command line.
% To close the created figures from the command line, you have to use "close all hidden" instead of "close all".
% Units Units for positioning plotECG in the parent figure. (default: normalized)
% Position Position of plotECG in the parent figure. (default: [0,0,1,1])
%
%
% Output Arguments
% h Returns the chart line objects as a vector. Use h to modify a chart line after it is created.
% hs Returns a struct with handles to some more GUI objects for later modifiaction
%
% You can set Name-Value parameters on the returned chart line handles:
% set(h, 'LineWidth',3) etc.
%
% Furthermore, you might want to change the X/YLabel:
% hs.ax.XLabel.String = 'Seconds'; or: xlabel(hs.ax,'Seconds')
%
%
%
% Example
% X = 0:0.001:100-0.001;
% Y = sin(2*pi*0.1*X) + sin(2*pi*X) + .1*sin(2*pi*50*X);
% [h,hs] = plotECG(X,Y);
% [h,hs] = plotECG(1000,Y, 'Filter','filter_FFT');
%
%
% Filter Function
% A filter function (@filter) can be specified that modifies Y.
% It must have at least two inputs - X and Y. X can be the scalar
% sampling rate or a time vector; Y can be a vector containing one signal
% or a matrix with multiple signals along its columns.
% In addition, @filter can have any number of scalar inputs that specify
% alterable parameters like cutoff frequencies of filters.
% For each of these parameters, a slider and a text edit appears in the
% plot figure, where the value can be adjusted with direct feedback.
% The filter function file can be modified while the plot window is open.
% A refiltering can be triggered by hitting enter in a text edit.
% @filter must have three output arguments: the modified X and Y and a
% description struct containing information about the filter function
% and its parameters. An example filter function with two parameters
% and its description struct is given below:
%
% function [ time_out, sig_out, description ] = filter_bandpass( time_in, sig_in, highpass, lowpass )
% % --- Description struct ---
% % Find samplerate
% if isscalar(time_in), samplerate=time_in; else samplerate=(length(time_in)-1)/(time_in(end)-time_in(1)); end
% % Filter title
% description.string = 'Butterworth bandpass';
% % Slider for lower cutoff frequency
% description.slider(1).Label = 'highpass';
% description.slider(1).Min = 0.001;
% description.slider(1).Value = 0.2;
% description.slider(1).Max = 4;
% % Slider for higher cutoff frequency
% description.slider(2).Label = 'lowpass';
% description.slider(2).Min = min( 5,samplerate*0.01);
% description.slider(2).Value = min( 50,samplerate/2.01);
% description.slider(2).Max = min(200,samplerate/2.01);
% % If an empty or too short signal is given, just return the description struct
% if size(sig_in,1)<=12; time_out=time_in; sig_out=NaN*sig_in; return; end
%
% % --- Signal processing ---
% bpFilt = designfilt('bandpassiir', ...
% 'FilterOrder',2, ...
% 'HalfPowerFrequency1',highpass, ...
% 'HalfPowerFrequency2',lowpass, ...
% 'SampleRate',samplerate);
% sig_out = filtfilt(bpFilt,sig_in);
% time_out = time_in;
% end
%
%
%
%
% Changes
% Ver. 1.1.1
% The Matlab Data Cursor works now too, if you switch it on. Of course you can still use the built-in InformationList.
% (However the scroll wheel does not work as long as Data Cursor Mode is on.)
% Replaced the "<HTML>" tags with "<html>" because MATLAB Online uicontrol listbox doesn't support uppercase HTML tags.
% Replaced the dash with a hyphen in the file's documentation because MATLAB editor on Mac doesn't support unicode.
%
%
%
%
% TODO
% Export tight images (PDF, PNG) via menu bar
% Multiple filters for signal
% Add scaling information bars into figure
% Currently no line if panned by hand instead of slider
% Enable signal data update during runtime (return function handle)
% Test thoroughly with MATLAB Online and Linux
%
%
%
%% Dependencies
% [flist,plist] = matlab.codetools.requiredFilesAndProducts('plotECG.m'); [flist'; {plist.Name}']
% plotECG(): no dependencies
% built-in local filter function 'filter_bandpass_notch': Signal Processing Toolbox
%% Parse Inputs
args = varargin;
iSig = 1;
SIG = struct;
defaultXLabel = 'Time in s';
mm_default = 50;
if length(args)>=2 && isnumeric(args{1}) && isnumeric(args{2})
% plotECG(X,Y)
SIG.X0 = args{1};
SIG.Y0 = args{2};
args = args(3:end);
elseif length(args)>=1 && isnumeric(args{1})
% plotECG(Y)
defaultXLabel = '';
SIG.Y0 = args{1};
SIG.X0 = 1;
args = args(2:end);
mm_default = 0.001;
else
% plotECG()
show_demo();
return;
end
% Check X and Y and change to column-oriented data
if numel(SIG.X0)==1 % X = sample rate
validateattributes(SIG.X0,{'double'},{'nonempty','real','finite','scalar','positive' }, 'plotECG','scalar X',1)
else % X = timestamps
validateattributes(SIG.X0,{'double'},{'nonempty','real','finite','vector','nondecreasing'}, 'plotECG','vector X',1)
end
validateattributes(SIG.Y0,{'double'},{'nonempty','real','2d'}, 'plotECG','Y')
if size(SIG.X0,1)==1, SIG.X0=SIG.X0'; end % change to column vector
if isrow(SIG.Y0) || ~isscalar(SIG.X0) && size(SIG.Y0,1)~=size(SIG.X0,1)
SIG.Y0=SIG.Y0'; % each column must be one signal
end
assert(isscalar(SIG.X0) || size(SIG.Y0,1)==size(SIG.X0,1),'Y must have dimensions such that one of its dimensions equals length(X) if X is not a scalar')
assert(size(SIG.Y0,1)>1, 'Signal must have at least two samples')
assert(nnz(~isfinite(SIG.Y0))<numel(SIG.Y0),'Y completely consists of infinite values')
if nnz(~isfinite(SIG.Y0))>.5*numel(SIG.Y0), warning('Y contains %.2f %% infinite values\n',nnz(~isfinite(SIG.Y0))/numel(SIG.Y0)*100); end
assert(nargout<=2,'Maximum 2 (instead of %u) output arguments are possible',nargout)
% % Works already with piecewise equally-spaced signals
% % TODO test with non-equally spaced signals
% if ~isscalar(SIG.X0)
% x0_diff = diff(SIG.X0);
% variation = (max(x0_diff)-min(x0_diff)) / max(abs(SIG.X0));
% assert(variation<eps*1000, 'X must be equally spaced.')
% end
if isscalar(SIG.X0)
period = 1/SIG.X0;
else
period = median(diff(SIG.X0),'omitnan');
%period = (SIG.X0(end)-SIG.X0(1))/(length(SIG.X0)-1);
end
assert(period>0,'The sampling period must be > 0')
if isscalar(SIG.X0)
timeBoundary = [0, period*(size(SIG.Y0,1)-1)];
else
xFinite = isfinite(SIG.X0);
timeBoundary = [SIG.X0(find(xFinite,1,'first')),SIG.X0(find(xFinite,1,'last'))];
end
duration = diff(timeBoundary);
assert(duration>0,'The duration must be > 0')
lineSpec = '-';
if size(SIG.Y0,2)==1
lineSpec = '-k';
end
if length(args)>=1 && isLineSpec(args{1})
lineSpec = args{1};
args = args(2:end);
end
parser = inputParser;
parser.FunctionName = 'plotECG';
%parser.KeepUnmatched = true; % additional Matlab plot arguments
% Classes:
% numeric; double; single; int8; int16; int32; int64; uint8; uint16; uint32; uint64;
% logical; char; struct; cell; function_handle;
% Attributes:
% binary; integer; even; odd; real; finite; nonnan;
% positive; nonnegative; nonzero; >,N; >=,N; <,N; <=,N;
% decreasing; increasing; nondecreasing; nonincreasing;
% nonempty, numel,N; ncols,N; nrows,N; ndims,N; square, nonsparse;
% scalar; vector; column; row; 2d; 3d; size,[d1,...,dN];
%
parser.addParameter('Filter' , 'none' , @(x)validateattributes(x,{'char','function_handle'},{'vector','nonempty'}))
parser.addParameter('mmPerSec' , mm_default , @(x)validateattributes(x,{'double'},{'real','finite','positive','scalar'}))
parser.addParameter('secPerScreenWidth' , 1 , @(x)validateattributes(x,{'double'},{'real','finite','positive','scalar'}))
parser.addParameter('ShowAxisTicks' , 'on' , @(x)any(validatestring(x,{'on','off'})))
parser.addParameter('ShowInformationList', 'none' , @(x)validateattributes(x,{'char','function_handle'},{'vector','nonempty'}))
parser.addParameter('AutoStackSignals' , {} , @(x)iscellstr(x))
parser.addParameter('SecondXAxisFunction', 'none' , @(x)validateattributes(x,{'char','function_handle'},{'vector','nonempty'}))
parser.addParameter('SecondXAxisLabel' , '' , @(x)validateattributes(x,{'char'},{}))
parser.addParameter('YLimMode' , 'dynamic' , @(x)any(validatestring(x,{'dynamic','fixed'})))
parser.addParameter('AutoMarkers' , '.' , @(x)any(validatestring(x,{'+','o','*','.','x','square','diamond','v','^','>','<','pentagram','hexagram','none'})))
parser.addParameter('ColorOrder' , [] , @(x)validateattributes(x,{'double'},{'real','finite','nonnegative', '<=',1, 'size',[NaN,3]}))
parser.addParameter('ScrollCallback' , 'none' , @(x)validateattributes(x,{'char','function_handle'},{'vector','nonempty'}))
parser.addParameter('Parent' , 0 , @(x)isscalar(x) && isgraphics(x) && x~=0)
parser.addParameter('Units' , 'normalized', @(x)any(validatestring(x,{'pixels','normalized','inches','centimeters','points','characters'})))
parser.addParameter('Position' , [0,0,1,1] , @(x)validateattributes(x,{'double'},{'real','finite','nonnegative', 'size',[1 4]}))
parser.parse(args{:})
if ~strcmp(parser.Results.Filter,'none')
% Check if function works with empty signal input
feval(parser.Results.Filter,1,[]);
% Check if function has three output arguments
assert(nargout(parser.Results.Filter)==3,'Filter %s must have 3 output arguments (instead of %u)',func2str2(parser.Results.Filter),nargout(parser.Results.Filter))
end
SIG.AutoStackSignals = parser.Results.AutoStackSignals;
if ~isempty(SIG.AutoStackSignals)
nStr = numel(SIG.AutoStackSignals);
nSig = size(SIG.Y0,2);
assert(nStr==nSig, 'You specified %u Strings in ''AutoStackSignals'', but the number of signals in Y is %u.',nStr,nSig);
end
%% Add the GUI components
mmPerSec = [];
secPerScreenWidth = [];
if ismember('secPerScreenWidth',parser.UsingDefaults)
mmPerSec = parser.Results.mmPerSec;
else
secPerScreenWidth = parser.Results.secPerScreenWidth;
end
mmPerSec_slider = mmPerSec;
% N = size(SIG.Y0,1); % number of data points
N = round(duration / period)+1;
axWidth_cm = 100;
axWidth_px = 100;
% Layout constants
fontSize = 11;
units = 'centimeters';
space = 0.05;
sliderHeight = .5;
checkboxHeight = 1;
editWidth = 2;
% Add components, save handles in a struct
hs = struct;
if parser.Results.Parent==0
% Create new figure
% TODO make initial figure height dependent on number of filters; voltage scaling
hs.parent = figure('Units','normalized', 'OuterPosition',[0.3,0.53,0.65,0.45], 'HandleVisibility','Callback');
else
hs.parent = parser.Results.Parent;
end
% Find parent figure
hs.fig = hs.parent;
while ~isempty(hs.fig) && ~strcmp('figure', get(hs.fig,'type'))
hs.fig = get(hs.fig,'parent');
end
% Disable all interactive modes.
% Only then the WindowScrollWheelFcn can be set.
rotate3d(hs.fig,'off')
zoom(hs.fig,'off')
pan(hs.fig,'off')
% Now set custom WindowScrollWheelFcn
hs.fig.WindowScrollWheelFcn = @figScroll;
hs.panel = uipanel(... % This uipanel can be put into another GUI
'Parent',hs.parent,...
'Units',parser.Results.Units,...
'Position',parser.Results.Position,...
'BorderWidth',0,...
'SizeChangedFcn',@resizegui,...
'Visible','off');
hs.ax2 = axes(...
'Parent',hs.panel,...
'ActivePositionProperty','Position',...
'XAxisLocation','top',...
'YAxisLocation','right',...
'YTickLabel',{''},...
'Color','none');
if strcmp(parser.Results.SecondXAxisFunction,'none')
set(hs.ax2,'Visible','off')
end
hs.ax = axes(...
'Parent',hs.panel,...
'TickLabelInterpreter','none',...
'ActivePositionProperty','Position');
if ~isempty(parser.Results.ColorOrder)
hs.ax.ColorOrder = parser.Results.ColorOrder;
hs.ax.NextPlot = 'replacechildren';
end
hs.scroll = uicontrol(...
'Parent',hs.panel,...
'Style','slider',...
'Min',0,...
'Value',0,...
'Max',1,...
'SliderStep',[1e-4,.07],...
'TooltipString','Click the slider trough or ouse mouse wheel to page forward one screen width.',...
'Interruptible','on',...
'Callback',{@redraw,true});
hListener = addlistener(hs.scroll,'ContinuousValueChange',@redraw);
setappdata(hs.scroll,'sliderListener',hListener);
hs.zoom = uicontrol(...
'Parent',hs.panel,...
'Style','slider',...
'Min',0,...
'Value',.5,...
'Max',1,...
'SliderStep',[1e-4,.07],...
'TooltipString','Zoom',...
'Interruptible','on',...
'Callback',{@redraw,true});
hListener = addlistener(hs.zoom,'ContinuousValueChange',@zoom_callback);
setappdata(hs.zoom,'sliderListener',hListener);
function zoom_callback(varargin)
redraw(varargin{:});
mmPerSec_slider = (axWidth_cm*10)/(numPoints*period);
end
d = -log(N/7);
hs.list = uicontrol(...
'Parent',hs.panel,...
'Style','listbox',...
'Max',2,...
'FontSize',fontSize,...
'Value',[]);
if strcmpi(parser.Results.ShowInformationList,'none')
hs.list.Visible = 'off';
end
%% Add Filter Controls
if ~strcmp(parser.Results.Filter,'none') % a filter function was specified
SIG.filter.hFilter = parser.Results.Filter;
% Get the filter description by passing it an empty signal
dummy = cell(1,nargin(SIG.filter.hFilter));
dummy{1} = SIG.X0; % for samplerate detection
[~,~,description] = feval(SIG.filter.hFilter, dummy{:});
% Find the function's file
filterFile = function_file(SIG.filter.hFilter);
SIG.filter.panel = uipanel(...
'Parent',hs.panel,...
'FontSize',fontSize,...
'BorderType','none',...
'Title',filterFile);
nControls = nargin(SIG.filter.hFilter)-2;
SIG.filter.mainCheck = uicontrol(...
'Parent',SIG.filter.panel,...
'Style','checkbox',...
'Value',0,...
'FontSize',fontSize,...
'Callback',@updateEnabled,...
'String',description.string);
% Format of description
% description.slider(1).Label = 'f_low';
% description.slider(1).Min = 0.01;
% description.slider(1).Value = 0.5;
% description.slider(1).Max = 3;
% Create the sliders
% TODO check 'description' better
assert(length(description.slider)==nControls,'Error in filter function ''%s'': 3rd output argument description.slider has %u entries where the function has %u user control inputs.',func2str2(SIG.filter.hFilter),length(description),nControls)
for iControl = 1:nControls
assert(description.slider(iControl).Min>0 , 'Due to logarithmic slider scaling, the slider ranges must be geater than zero.')
assert(description.slider(iControl).Value>0, 'Due to logarithmic slider scaling, the slider ranges must be geater than zero.')
assert(description.slider(iControl).Max>0 , 'Due to logarithmic slider scaling, the slider ranges must be geater than zero.')
SIG.filter.slider(iControl).text = uicontrol(...
'Parent',SIG.filter.panel,...
'Style','text',...
'FontSize',fontSize,...
'String',description.slider(iControl).Label);
SIG.filter.slider(iControl).edit = uicontrol(...
'Parent',SIG.filter.panel,...
'FontSize',fontSize,...
'String',num2str(description.slider(iControl).Value,4),...
'Style','edit');
SIG.filter.slider(iControl).slider = uicontrol(...
'Parent',SIG.filter.panel,...
'Style','slider',...
'FontSize',fontSize,...
'Interruptible','on',...
'Min',description.slider(iControl).Min,...
'Max',description.slider(iControl).Max,...
'SliderStep',[1e-4,.1]);
editToSlider(SIG.filter.slider(iControl).edit,SIG.filter.slider(iControl).slider);
% Assemble callbacks
% Edit to slider; updateFilter
set(SIG.filter.slider(iControl).edit, 'Callback', {@editToSliderUpdateFilter, SIG.filter.slider(iControl).edit, SIG.filter.slider(iControl).slider, iSig});
% Slider to edit as continuous update; no updateFilter
sliderToEdit2 = @(varargin) sliderToEdit(SIG.filter.slider(iControl).edit,SIG.filter.slider(iControl).slider);
hListener = addlistener(SIG.filter.slider(iControl).slider,'ContinuousValueChange',sliderToEdit2);
setappdata(SIG.filter.slider(iControl).slider,'sliderListener',hListener);
sliderToEdit2();
% Slider to edit on mouse release; updateFilter
set(SIG.filter.slider(iControl).slider, 'Callback', {@sliderToEditUpdateFilter, SIG.filter.slider(iControl).edit, SIG.filter.slider(iControl).slider, iSig});
end
% Use default value for first calculation
sliderToEditUpdateFilter([],[], SIG.filter.slider(iControl).edit, SIG.filter.slider(iControl).slider, iSig);
end
%% Create chart line handle
click_info.x = [];
click_info.y = [];
%hs.line = plot(hs.ax,1,1:size(SIG.Y0,2),lineSpec,parser.Unmatched); % Unmatched name-value pairs as plot parameters
hs.line = plot(hs.ax,1,1:size(SIG.Y0,2),lineSpec); % Safer: Don't allow unmatched name-value pairs. Plot can still be modified by handles.
% Remove 10^x axes factor
hs.ax2.XAxis.Exponent = 0;
hs.ax2.XAxis.ExponentMode = 'manual';
hs.ax2.YAxis.Exponent = 0;
hs.ax2.YAxis.ExponentMode = 'manual';
hs.ax.XAxis.Exponent = 0;
hs.ax.XAxis.ExponentMode = 'manual';
hs.ax.YAxis.Exponent = 0;
hs.ax.YAxis.ExponentMode = 'manual';
if strcmpi(parser.Results.ShowAxisTicks,'on')
hs.ax.XLabel.String = defaultXLabel;
hs.ax.TickLength = [0.001,0.001];
hs.ax2.TickLength = [0.001,0.001];
hs.ax2.XLabel.String = parser.Results.SecondXAxisLabel;
hs.ax.XMinorGrid = 'on';
if isempty(parser.Results.AutoStackSignals)
hs.ax.YLabel.String = 'Voltage in mV';
else
hs.ax.YLabel.String = 'Channel';
end
else
set(hs.ax ,'XTick',[], 'YTick',[])
set(hs.ax2,'XTick',[], 'YTick',[])
end
if ~isempty(SIG.AutoStackSignals) && strcmp(parser.Results.YLimMode,'fixed')
% Stack signals horizontally.
[sigPosVec,sigAddVec] = auto_stack_nooverlap(SIG.Y0);
hs.ax.YTick = flip(sigPosVec);
hs.ax.YTickLabel = flip(SIG.AutoStackSignals(:));
hs.ax.TickLabelInterpreter = 'none';
else
sigPosVec = zeros(1,size(SIG.Y0,2));
sigAddVec = zeros(1,size(SIG.Y0,2));
end
Y0pos = bsxfun(@plus,SIG.Y0,sigAddVec);
range = [min(Y0pos(:)),max(Y0pos(:))];
dlt = diff(range)/50;
range(1) = range(1)-dlt;
range(2) = range(2)+dlt;
if strcmp(parser.Results.YLimMode,'fixed') && nnz(isnan(range))==0 && range(2)>range(1)
hs.ax .YLimMode = 'manual';
hs.ax2.YLimMode = 'manual';
hs.ax. YLim = range;
hs.ax2.YLim = range;
end
if ~strcmp(parser.Results.Filter,'none')
updateEnabled()
end
% Make figure visible after adding components
btnDown()
redraw(true);
try getframe(hs.fig); catch, end % update system queue
resizegui
if ~isempty(mmPerSec)
numPoints = axWidth_cm*10/(mmPerSec*period);
else
numPoints = secPerScreenWidth/period;
end
zoomValue = log(numPoints/N)/d;
zoomValue = max(zoomValue,0);
zoomValue = min(zoomValue,1);
set(hs.zoom,'Value',zoomValue)
zoomValue = hs.zoom.Value;
resizegui
redraw(true)
hs.panel.Visible = 'on';
%% Updatefunctions
function value = editToSliderUpdateFilter(ho,cbd, edit,slider,iSig)
value = editToSlider(edit,slider);
updateFilter(ho,cbd, iSig);
end
function value = sliderToEditUpdateFilter(ho,cbd, edit,slider,iSig)
value = sliderToEdit(edit,slider);
updateFilter(ho,cbd, iSig);
end
function updateFilter(ho,~, iSig)
nCtrl = length(SIG.filter.slider);
parameters = cell(nCtrl,1);
for iCtrl = 1:nCtrl
parameters{iCtrl} = str2double(SIG.filter.slider(iCtrl).edit.String);
end
function arrowset(fig)
fig.Pointer = 'arrow';
end
if ~strcmp(hs.fig.Pointer,'watch')
cleaner = onCleanup(@() arrowset(hs.fig));
hs.fig.Pointer = 'watch';
drawnow;
end
% Filter debugging with "dbstop if error" works better without a try/rethrow in the stack.
% But for end users you can uncomment this try/catch block
% so if they type improper values in the filter text boxes, the filter panel will turn red.
% try
[SIG.X, SIG.Y, ~] = feval(SIG.filter.hFilter, SIG.X0, SIG.Y0, parameters{:});
if nnz(isfinite(SIG.X))<nnz(SIG.X); warning('Filtered X contains %u nonfinite values\n',nnz(SIG.X)-nnz(isfinite(SIG.X))); end
if nnz(isfinite(SIG.Y))<nnz(SIG.Y); warning('Filtered Y contains %u nonfinite values\n',nnz(SIG.Y)-nnz(isfinite(SIG.Y))); end
assert(isreal(SIG.Y), 'Signal procuded by filter must be real')
SIG.filter.panel.BackgroundColor = [.94 .94 .94];
SIG.filter.mainCheck.BackgroundColor = [.94 .94 .94];
% catch ex
% fprintf('The filter produced an error:\n')
% SIG.filter.panel.BackgroundColor = 'red';
% SIG.filter.mainCheck.BackgroundColor = 'red';
% hs.fig.Pointer = 'arrow';
% rethrow(ex)
% end
hs.fig.Pointer = 'arrow';
if ~isempty(ho)
redraw(true);
end
end
function updateEnabled(varargin)
if SIG.filter.mainCheck.Value == SIG.filter.mainCheck.Max
enable = 'on';
else
enable = 'off';
end
nCtrl = length(SIG.filter.slider);
for iCtrl = 1:nCtrl
SIG.filter.slider(iCtrl).text.Enable = enable;
SIG.filter.slider(iCtrl).edit.Enable = enable;
SIG.filter.slider(iCtrl).slider.Enable = enable;
end
redraw(true);
end
function redraw(varargin)
if ~ishandle(hs.line(1)) && length(varargin)>1
% figure overplotted by normal plot()
return
end
scrollValue = get(hs.scroll,'Value');
%fprintf('scrollValue: %f\n',scrollValue)
zoomValue = get(hs.zoom,'Value');
% zoomValue==0: numPoints=N
% zoomValue==1: numPoints=7
% N * exp(d*x)
numPoints = N*exp(d*zoomValue);
numPoints = round(numPoints);
numPoints = max(numPoints,2);
% scrollValue==0: startIndex=1;
% scrollValue==1: startIndex=N-numPoints+1;
startIndex = (N-numPoints)*scrollValue+1; % m*x+b
endIndex = startIndex+numPoints;
if ~isscalar(SIG.X0)
startTime = timeBoundary(1)+period*(startIndex-1);
[~,startIndex] = min(abs(startTime-SIG.X0));
endTime = timeBoundary(1)+period*(endIndex-1);
[~,endIndex] = min(abs(endTime-SIG.X0));
end
startIndex = round(startIndex);
endIndex = round(endIndex);
startIndex = max(startIndex,1);
endIndex = min(endIndex,N);
% Maximum factor_max values per pixel,
% so very long signals don't hang Matlab
factor = round(numPoints/max(1,axWidth_px));
factor_max = 1000; % increase this if you want to find the "needle in the haystack" (single outlier sample)
if factor>factor_max
spc = floor(factor/factor_max);
else
spc = 1;
end
ind = startIndex:spc:endIndex;
%fprintf('spc: %f\n',spc)
% Use original or filtered signal according to checkbox state
if isfield(SIG,'filter') && SIG.filter.mainCheck.Value == SIG.filter.mainCheck.Max
if isscalar(SIG.X), XData=1/SIG.X*(ind-1); else, XData=SIG.X(ind); end
YData = SIG.Y(ind,:);
else
if isscalar(SIG.X0), XData=period*(ind-1); else, XData = SIG.X0(ind); end
YData = SIG.Y0(ind,:);
end
if isscalar(SIG.X0)
startTime = XData(1);
endTime = XData(end);
end
% Don't show much more samples than pixels.
% Make sure that minimum and maximum data is shown anyway
% (except if factor is > factor_max, for responsiveness)
maxSamplesPerPixel = 2;
if size(YData,1)/(axWidth_px*maxSamplesPerPixel) > 2
factor = ceil(size(YData,1)/(max(1,axWidth_px)*maxSamplesPerPixel));
remove = mod(size(YData,1),factor);
XData = XData(1:(end-remove));
YData = YData(1:(end-remove),:);
XData = reshape(XData,factor,[]);
XData = [min(XData,[],1);max(XData,[],1)];
XData = XData(:)';
YData = permute(YData,[3,1,2]);
YData = reshape(YData,factor,[],size(YData,3));
YData = [min(YData,[],1,'includenan');max(YData,[],1,'includenan')]; % preserves 'NaN' separations
YData = [min(YData,[],1');max(YData,[],1)];
YData = reshape(YData,[],size(YData,3));
end
% On the other hand, if there are much less samples than pixels,
% show additional dots
factor = size(YData,1)/max(1,axWidth_px);
if ~strcmp(parser.Results.AutoMarkers,'none')
if factor<0.2
set(hs.line, 'Marker',parser.Results.AutoMarkers);
else
set(hs.line, 'Marker','none');
end
end
if ~isempty(SIG.AutoStackSignals) && ~strcmp(parser.Results.YLimMode,'fixed')
% Stack signals horizontally dynamically
[sigPosVec,sigAddVec] = auto_stack(YData);
hs.ax.YTick = flip(sigPosVec);
hs.ax.YTickLabel = flip(SIG.AutoStackSignals(:));
hs.ax.TickLabelInterpreter = 'none';
end
YData = bsxfun(@plus,YData,sigAddVec);
% hs.ax2 limits
if ~strcmp(parser.Results.SecondXAxisFunction,'none')
ax2Limits = [feval(parser.Results.SecondXAxisFunction,startTime), feval(parser.Results.SecondXAxisFunction,endTime)];
if ax2Limits(1)<ax2Limits(2)
set(hs.ax2,'XDir','normal')
else
ax2Limits = flip(ax2Limits);
set(hs.ax2,'XDir','reverse')
end
set(hs.ax2,'XLim',ax2Limits)
end
set(hs.line,'XData',XData);
for iLine = 1:size(YData,2)
set(hs.line(iLine),'YData',YData(:,iLine));
end
set(hs.ax,'XLim',[startTime,endTime])
minY = min(YData(:));
maxY = max(YData(:));
delta = (maxY-minY)/50;
minY = minY-delta;
maxY = maxY+delta;
if nnz(sigPosVec)>1
minY = min([minY;sigPosVec(:)]);
maxY = max([maxY;sigPosVec(:)]);
end
if strcmp(parser.Results.YLimMode,'dynamic') && nnz(isnan([minY,maxY]))==0 && maxY>minY
set(hs.ax2,'YLim',[minY,maxY])
set(hs.ax,'YLim',[minY,maxY])
end
% Big scrollbar if there is nothing to scroll
if N<=numPoints
majorStep = Inf;
minorStep = .1;
else % N > numPoints
majorStep = max(1e-6,numPoints/(N-numPoints));
% 100 steps per screen width
minorStep = max(1e-6,(endTime-startTime)/(100*duration));
end
set(hs.scroll,'SliderStep',[minorStep,majorStep]);
if ~strcmp(parser.Results.ScrollCallback,'none')
arg.XLim = [startTime;endTime];
arg.ContinuousValueChange = ~isempty(varargin) && ~(islogical(varargin{end}) && varargin{end});
arg.hs = hs;
feval(parser.Results.ScrollCallback,arg);
end
end
function resizegui(varargin)
panelUnits = hs.panel.Units;
% Centimeter layout
set(hs.panel ,'Units',units);
set(hs.ax ,'Units',units);
set(hs.ax2 ,'Units',units);
set(hs.list ,'Units',units);
set(hs.scroll,'Units',units);
set(hs.zoom ,'Units',units);
width = hs.panel.Position(3);
height = hs.panel.Position(4);
yPos = space;
% Filter layout
if isfield(SIG,'filter')
filterHeight = layoutFilter(iSig,1,width-2*space);
pos = [space,yPos,width-2*space,filterHeight];
pos = [pos(1), pos(2), max(0,pos(3)), max(0,pos(4))];
set(SIG.filter.panel, 'Units',units, 'Position',pos);
yPos = pos(2)+pos(4)+4*space;
end
% Zoom slider
pos = [space,yPos,width-2*space,sliderHeight];
pos = [pos(1), pos(2), max(0,pos(3)), max(0,pos(4))];
set(hs.zoom, 'Units',units, 'Position',pos)
yPos = pos(2)+pos(4)+space;
% Scroll slider
pos = [space,yPos,width-2*space,sliderHeight];
pos = [pos(1), pos(2), max(0,pos(3)), max(0,pos(4))];
set(hs.scroll, 'Units',units, 'Position',pos)
yPos = pos(2)+pos(4)+space;
% List (I)
if strcmpi(parser.Results.ShowInformationList,'none')
listWidth = 0;
else
listWidth = 3;
listWidth = min(listWidth,width/5);
end
% Axis
if strcmpi(parser.Results.ShowAxisTicks,'on')
insets = get(hs.ax,'TightInset');
if isequal(hs.ax2.Visible,'on')
insets = insets + get(hs.ax2,'TightInset');
end
else
insets = [0,0,0,0];
end
pos = [space,yPos,max(1,width-3*space-listWidth),max(1,height-yPos)];
pos = [pos(1)+insets(1), pos(2)+insets(2), pos(3)-insets(1)-insets(3), pos(4)-insets(2)-insets(4)];
pos = [pos(1), pos(2), max(0,pos(3)), max(0,pos(4))];
set(hs.ax, 'Units',units, 'Position',pos)
set(hs.ax2, 'Units',units, 'Position',pos)
set(hs.panel,'Units',panelUnits);
% List (II)
if ~strcmpi(parser.Results.ShowInformationList,'none')
axPos = hs.ax.Position;
pos = [width-space-listWidth,axPos(2),listWidth,axPos(4)];
set(hs.list, 'Units',units, 'Position',pos)
end
% Update axWidth_cm and axWidth_px
set(hs.ax,'Units','centimeters');
axWidth_cm=get(hs.ax,'Position'); axWidth_cm=axWidth_cm(3); axWidth_cm=max(axWidth_cm,0);
set(hs.ax,'Units','pixels');
axWidth_px=get(hs.ax,'Position'); axWidth_px=round(axWidth_px(3)); axWidth_px=max(axWidth_px,0);
% Change zooming such that mmPerSec stays the same
if ~isempty(varargin) && ~isempty(mmPerSec) % do this only for calls by GUI, not during the initialization call
numPoints = axWidth_cm*10/(mmPerSec_slider*period);
zoomValue = log(numPoints/N)/d;
zoomValue = max(zoomValue,0);
zoomValue = min(zoomValue,1);
set(hs.zoom,'Value',zoomValue)
redraw(true)
end
end
function height = layoutFilter(~,~,width)
yPos = space;
% Layout the sliders and their text fields from bottom to top
nCtrl = length(SIG.filter.slider);
% Find maximum label width
labelWidth = 1;
for iCtrl = 1:nCtrl
ext = SIG.filter.slider(iCtrl).text.Extent(3);
if ext > labelWidth
labelWidth = ext;
end
end
for iCtrl = flip(1:nCtrl)
xPos = space;
% Text label
pos = [xPos,yPos,labelWidth,sliderHeight];
pos = [pos(1), pos(2), max(0,pos(3)), max(0,pos(4))];
set(SIG.filter.slider(iCtrl).text, 'Units',units, 'Position',pos);
xPos=pos(1)+pos(3)+space;
% Edit box
pos = [xPos,yPos,editWidth,sliderHeight];
pos = [pos(1), pos(2), max(0,pos(3)), max(0,pos(4))];
set(SIG.filter.slider(iCtrl).edit, 'Units',units, 'Position',pos);
xPos=pos(1)+pos(3)+space;
% Slider
pos = [xPos,yPos,width-xPos-space,sliderHeight];
pos = [pos(1), pos(2), max(0,pos(3)), max(0,pos(4))];
set(SIG.filter.slider(iCtrl).slider, 'Units',units, 'Position',pos);
% New line
yPos=pos(2)+pos(4)+space;
end
% Layout the checkbox
yPos = yPos-3*space;
pos = [space,yPos,width-2*space,checkboxHeight];
pos = [pos(1), pos(2), max(0,pos(3)), max(0,pos(4))];
set(SIG.filter.mainCheck, 'Units',units, 'Position',pos);
yPos = pos(2)+pos(4)+space;
height = yPos+3*space;
end
function figScroll(~,callbackdata)
scrollCount = callbackdata.VerticalScrollCount;
val = hs.scroll.Value + scrollCount*hs.scroll.SliderStep(2);
val = max(hs.scroll.Min,val);
val = min(hs.scroll.Max,val);
hs.scroll.Value = val;
redraw(true);
end
function btnDown(varargin)
% x and y location where button was pressed is stored
% ShowInformationList function is called with these locations
% and the returned strings are displayed.
% Numbers are converted to strings.
x = hs.ax.CurrentPoint(1,1);
y = hs.ax.CurrentPoint(1,2);
click_info.x = [x;click_info.x];
click_info.y = [y;click_info.y];
%std_InformationList(hs,click_info)
if strcmpi(parser.Results.ShowInformationList,'none')
str = {};
else
str = feval(parser.Results.ShowInformationList, hs,click_info);
end
assert(iscell(str),'ShowInformationList function must return a cell array.')
assert(isempty(str) || isvector(str), 'ShowInformationList function must return a cell vector.')
% Convert numbers to strings
for k = 1:length(str)
if ~ischar(str{k})
str{k} = num2str(str{k});
end
end
% Update uicontrol
hs.list.String = str;
end
if ~strcmpi(parser.Results.ShowInformationList,'none')
hs.ax.ButtonDownFcn = @btnDown;
%set(hs.line,'PickableParts','none') % DataCursor doesn't work anymore if you use this.
set(hs.line,'ButtonDownFcn',@btnDown) % DataCursor mode puts its own callback here as long as it is enabled.
end
function delete_plotECG(varargin)
%fprintf('delete_plotECG\n')
SIG = [];
click_info = [];
if isvalid(hs.fig)
hs.fig.WindowScrollWheelFcn = '';
end
end
hs.ax.DeleteFcn = @delete_plotECG;
hs.panel.DeleteFcn = @delete_plotECG;
hs.zoom.DeleteFcn = @delete_plotECG;
%% Return output arguments
if nargout>=1
varargout{1} = hs.line;