-
Notifications
You must be signed in to change notification settings - Fork 0
/
crosshair_subplots.m
1128 lines (961 loc) · 40.2 KB
/
crosshair_subplots.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
function [Xpoint,Ypoint,XYhist] = crosshair_subplots(action);
% CROSSHAIR: A gui interface for reading (x,y) values from a plot.
%
% [Xpoint,Ypoint,XYhist] = crosshair_subplots(action);
%
% A set of mouse driven crosshairs is placed on the current axes,
% and displays the current (x,y) values of the line plot. There is an
% option to provide data specific values or interpolated values. The
% resolution of the data specific values depends on both the data
% resolution and the GUI interface (mainly mouse movement resolution).
% The interpolated values appear to provide a more continuous function,
% however they too depend on the GUI interface resolution. There are
% no options for extrapolation.
%
% For multiple traces, plots with the same length(xdata) are
% tracked. Each mouse click returns Xpoint,Ypoint values and selecting
% 'done' will remove the GUI and restore the mouse buttons to previous
% values. Selecting 'exit' will remove the GUI and close the figure.
%
% In this version (Dec 2002), the crosshairs can track multiple subplots
% and there are new options to define functions of X/Y and monitor
% their results. There is also a new STORE button that creates and
% updates an XYhist struct in the base workspace, which contains value
% labels and values. This version has better controls of X/Y movements,
% including better interpolation movement options. This version attempts
% to respond correctly to keyboard entries, including TAB between subplots,
% RETURN to 'save', ESC for 'done' and all the arrow keys to move the
% crosshairs.
%
% Some further help is given in the tool tips of the GUI.
%
% Note: crosshair should always update the Xpoint,Ypoint in the base
% workspace. Here is an example of how to get return values within
% a script/function after pressing the exit button of crosshair:
% function [X,Y] = crosshair_returnXY
% x = [1:10]; y(1,:) = sin(x); y(2,:) = cos(x);
% figure; plot(x,y); crosshair;
% uiwait
% X = evalin('base','Xpoint');
% Y = evalin('base','Ypoint');
% return
% Copy this text to a function .m file and then call it from the
% base workspace with [X,Y] = crosshair_returnXY
%
% Useage: wt=0:0.01:2.5*pi;
% t=wt;
% subplot(2,1,1),plot(t,1.0*sin(wt),t,1.0*sin(wt-110/180*pi),t,1.0*sin(wt-250/180*pi));
% subplot(2,1,2), plot(t,sin(2*wt));
% crosshair_subplots;
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Copyright (C) 2002 Darren L. Weber
%
% This program is free software; you can redistribute it and/or
% modify it under the terms of the GNU General Public License
% as published by the Free Software Foundation; either version 2
% of the License, or (at your option) any later version.
%
% This program is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
% GNU General Public License for more details.
%
% You should have received a copy of the GNU General Public License
% along with this program; if not, write to the Free Software
% Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
% History: 03/96, Richard G. Cobb <[email protected]>
% 08/01, [email protected]
% replaced obsolete 'table1' with 'interp1'; fixed bug
% with number of 'traces'; rationalized calculations into
% a common subfunction for x,y point calc in 'down','up',
% & 'move' button functions; added option to turn on/off
% interpolation and the exit button; simplified updates
% to graphics using global GUI handle structure.
% 11/01, [email protected]
% added tooltips for several GUI handles
% added multiple interpolation methods
% added GUI for data matrix indices (given no interpolation)
% added option to select trace nearest to mouse click point
% reversed order of lines in data matrix to be consistent
% with the value returned from the nearest trace subfunction
% create crosshair lines after finding all plot lines to
% avoid confusing them with the plot lines
% 01/02, [email protected]
% should now work across multiple plot figures, given
% that all gui handles and data are now stored in the
% plot figure 'userdata' handle.
% added functionality to move smoothly from interpolation
% back to the delimited data via the "next/previous"
% buttons.
% 06/02, [email protected]
% learned how to get values back to a script/function with
% evalin command and updated help above.
% 12/02, [email protected]
% added Store uicontrol and associated XYhist variable
% updates in base workspace to provide storage of
% consecutive XY values (thanks to [email protected] &
% [email protected] for their suggestion/assistance).
% added keyboard controls to left/right arrow and return.
% added prev/next X interpolation interval.
% added handling of subplots in crosshair_subplots version.
% 06/05, suggestion not implemented yet:
% I often use 2 or more vertically arranged sublots with the same x coordinates.
% Would it be possible to adapt your function to have vertical cursor displayed
% on each sublots with the same x coordinates? Of course y information would be
% written only for one sublot at a time(the selected one)!
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
if ~exist('action','var')
action = 'init';
elseif isempty(action)
action = 'init';
end
XHR_HANDLES = get(gcf,'userdata');
if gca & ~isempty(XHR_HANDLES),
if ~isequal(gca,XHR_HANDLES.axis),
% Ensure we have the right axis data
XHR_HANDLES.axis = gca;
XHR_HANDLES.data = get_data;
end
end
% Check for specific keys and assign reasonable actions
if strcmp(action, 'keypress'),
CC = get(XHR_HANDLES.gui,'CurrentCharacter');
cc = double(CC);
if cc,
switch cc,
case 9, action = 'axes'; % TAB, switch axes, if possible
case 27, action = 'done'; % ESC
case 28, action = 'prevx'; % left
case 29, action = 'nextx'; % right
case 30, action = 'ygt'; % up
case 31, action = 'ylt'; % down
case 13, action = 'store'; % return/enter
otherwise, action = 'up'; % all other keys
end
end
end
action = lower(action);
switch action,
case 'init',
% Paint GUI
XHR_HANDLES = INIT;
% Update and return values
XHR_HANDLES = updateDATA(XHR_HANDLES);
Xpoint = get(XHR_HANDLES.xvalue,'Value');
Ypoint = get(XHR_HANDLES.yvalue,'Value');
%updateXYhistory(Xpoint,Ypoint);
case 'axes',
% TAB between axes in subplots of a figure
ax = findobj(gcf,'type','axes');
if length(ax) > 1,
nax = find(ax == gca);
if nax < length(ax),
axes(ax(nax+1));
else
axes(ax(1));
end
end
XHR_HANDLES.axis = gca;
XHR_HANDLES.data = get_data;
XHR_HANDLES = updateGUI(XHR_HANDLES);
Xpoint = get(XHR_HANDLES.xvalue,'Value');
Ypoint = get(XHR_HANDLES.yvalue,'Value');
% Mouse Click Down
case 'down',
set(XHR_HANDLES.gui,'WindowButtonMotionFcn','crosshair_subplots(''move'');');
set(XHR_HANDLES.gui,'WindowButtonUpFcn','[Xpoint,Ypoint] = crosshair_subplots(''up'');');
XHR_HANDLES = updateDATA(XHR_HANDLES);
Xpoint = get(XHR_HANDLES.xvalue,'Value');
Ypoint = get(XHR_HANDLES.yvalue,'Value');
%updateXYhistory(Xpoint,Ypoint);
% Mouse Drag Motion
case 'move',
XHR_HANDLES = updateDATA(XHR_HANDLES);
Xpoint = get(XHR_HANDLES.xvalue,'Value');
Ypoint = get(XHR_HANDLES.yvalue,'Value');
%updateXYhistory(Xpoint,Ypoint);
% Mouse Click Up
case 'up',
set(XHR_HANDLES.gui,'WindowButtonMotionFcn',' ');
set(XHR_HANDLES.gui,'WindowButtonUpFcn',' ');
XHR_HANDLES = updateDATA(XHR_HANDLES);
Xpoint = get(XHR_HANDLES.xvalue,'Value');
Ypoint = get(XHR_HANDLES.yvalue,'Value');
%updateXYhistory(Xpoint,Ypoint);
% Next or Previous X point
case {'nextx','prevx','changex','nexty','prevy','changey','ylt','ygt'}, % Change X/Y
XHR_HANDLES = moveXY(XHR_HANDLES,action);
Xpoint = get(XHR_HANDLES.xvalue,'Value');
Ypoint = get(XHR_HANDLES.yvalue,'Value');
%updateXYhistory(Xpoint,Ypoint);
% Store XY values into a history array
case 'store',
Xpoint = get(XHR_HANDLES.xvalue,'Value');
Ypoint = get(XHR_HANDLES.yvalue,'Value');
updateXYhistory(XHR_HANDLES);
% Exit crosshairs GUI
case {'done','exit'},
Xpoint = get(XHR_HANDLES.xvalue,'Value');
Ypoint = get(XHR_HANDLES.yvalue,'Value');
%updateXYhistory(Xpoint,Ypoint);
handles = fieldnames(XHR_HANDLES);
for i=1:length(handles),
switch handles{i},
case {'axis','datalines','gui'},
otherwise,
h = getfield(XHR_HANDLES,handles{i});
if ishandle(h), delete(h); end
end
end
if strcmp(action,'exit');
if ishandle(XHR_HANDLES.gui),
close(XHR_HANDLES.gui);
end
else
lines = findobj(gcf,'tag','XHR_XLINE');
for l = 1:length(lines),
line = lines(l);
if ishandle(line), delete(line); end
end
lines = findobj(gcf,'tag','XHR_YLINE');
for l = 1:length(lines),
line = lines(l);
if ishandle(line), delete(line); end
end
set(XHR_HANDLES.gui,'WindowButtonUpFcn','');
set(XHR_HANDLES.gui,'WindowButtonMotionFcn','');
set(XHR_HANDLES.gui,'WindowButtonDownFcn',' ');
refresh(XHR_HANDLES.gui);
end
clear XHR_HANDLES;
return;
end
if ishandle(XHR_HANDLES.axis),
set(XHR_HANDLES.axis,'userdata',XHR_HANDLES.data);
end
if ishandle(XHR_HANDLES.gui),
set(XHR_HANDLES.gui,'userdata',XHR_HANDLES);
figure(XHR_HANDLES.gui);
end
return;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function updateXYhistory(H),
Ch = get(H.yindex,'Value');
X = get(H.xvalue,'Value');
Y = get(H.yvalue,'Value');
fX = get(H.fxvalue,'Value');
fY = get(H.fyvalue,'Value');
fXeq = get(H.fxeq,'String');
fYeq = get(H.fyeq,'String');
XY.labels = {'Channel','X','Y',['f(x) = ',fXeq],['f(y) = ',fYeq]};
XY.data = [Ch,X,Y,fX,fY];
if evalin('base','exist(''XYhist'',''var'');'),
XYhist = evalin('base','XYhist');
% get the current history set
set = getfield(XYhist,['set',num2str(XYhist.set)]);
if isequal(set.labels,XY.labels),
set.data(end+1,:) = XY.data;
XYhist = setfield(XYhist,['set',num2str(XYhist.set)],set);
assignin('base','XYhist',XYhist);
else
fprintf('\nWarning: creating new set of XYhist in base workspace.\n\n');
XYhist.set = XYhist.set + 1;
XYhist = setfield(XYhist,['set',num2str(XYhist.set)],XY);
assignin('base','XYhist',XYhist);
end
else
XYhist.set = 1;
XYhist.set1 = XY;
assignin('base','XYhist',XYhist);
end
return
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [H] = moveXY(H,move)
interp = get(H.interp,'Value');
xinterp = get(H.xinterp,'Value');
if (xinterp > 0) & (interp > 1),
% Use incremental interpolation of x values
switch move,
case 'nexty', H.data.yindex = H.data.yindex + 1;
case 'prevy', H.data.yindex = H.data.yindex - 1;
case 'ygt',
ydata = interpYall(H);
[ysort,yi] = sort(ydata);
currYI = find(ysort > H.data.ypoint);
if min(currYI),
H.data.yindex = yi(min(currYI));
end
case 'ylt',
ydata = interpYall(H);
[ysort,yi] = sort(ydata);
currYI = find(ysort < H.data.ypoint);
if max(currYI),
H.data.yindex = yi(max(currYI));
end
case 'nextx',
H.data.xpoint = H.data.xpoint + xinterp;
case 'prevx',
H.data.xpoint = H.data.xpoint - xinterp;
end
H = checkdatarange(H);
H = interpY(H);
updateGUI(H);
return
end
% No interpolation of x values...
if (interp > 1)
xdata = H.data.xdata(:,H.data.yindex);
[H.data.xindex] = NearestXYArrayPoint( xdata, H.data.xpoint, move );
end
switch move,
case 'nextx',
% Increase current xindex by one
if(interp == 1), H.data.xindex = H.data.xindex + 1; end
case 'prevx',
% Decrease current xindex by one
if(interp == 1), H.data.xindex = H.data.xindex - 1; end
case 'nexty', H.data.yindex = H.data.yindex + 1;
case 'prevy', H.data.yindex = H.data.yindex - 1;
case 'ygt',
ydata = H.data.ydata(H.data.xindex,:);
[ysort,yi] = sort(ydata);
currYI = find(ysort == H.data.ypoint);
if currYI < length(yi),
H.data.yindex = yi(currYI+1);
end
case 'ylt',
ydata = H.data.ydata(H.data.xindex,:);
[ysort,yi] = sort(ydata);
currYI = find(ysort == H.data.ypoint);
if currYI > 1,
H.data.yindex = yi(currYI-1);
end
otherwise
end
H = checkdatarange(H);
% Get x/y value at new x/y index
H.data.xpoint = H.data.xdata(H.data.xindex,H.data.yindex);
H.data.ypoint = H.data.ydata(H.data.xindex,H.data.yindex);
set(H.interp,'Value',1);
H = updateGUI(H);
return
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [ H ] = checkdatarange(H),
% Ensure that x/y index is within data range
s = size(H.data.xdata,1);
if( H.data.xindex < 1 ),
H.data.xindex = 1;
elseif( H.data.xindex >= s ),
H.data.xindex = s;
end
s = size(H.data.ydata,2);
if( H.data.yindex < 1 ),
H.data.yindex = 1;
elseif( H.data.yindex >= s ),
H.data.yindex = s;
end
return
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [ H ] = updateDATA( H )
% Only update if mouse pointer is in the
% axis limits
set(H.gui,'units','normalized');
axpos = get(H.axis,'position');
figcp = get(H.gui,'Currentpoint');
axlim = axpos(1) + axpos(3);
aylim = axpos(2) + axpos(4);
if or(figcp(1) > (axlim+.01), figcp(1) < (axpos(1)-.01)),
return;
elseif or(figcp(2) > (aylim+.01), figcp(2) < (axpos(2)-.01)),
return;
end
CurrentPoint = get(H.axis,'Currentpoint');
H.data.xpoint = CurrentPoint(1,1);
H.data.ypoint = CurrentPoint(1,2);
if get(H.traceNearest,'Value'),
% Get new yindex for nearest trace
[ H.data.xpoint, ...
H.data.xindex, ...
H.data.ypoint, ...
H.data.yindex ] = NearestXYMatrixPoint( H.data.xdata,...
H.data.ydata,...
H.data.xpoint,...
H.data.ypoint);
end
CurrentPoint = get(H.axis,'Currentpoint');
H.data.xpoint = CurrentPoint(1,1);
H.data.ypoint = CurrentPoint(1,2);
H = interpY(H);
H = updateGUI(H);
return
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [ H ] = updateGUI( H )
InterpMethod = get(H.interp,'Value');
if (InterpMethod > 1)
% There is no specific matrix x-index for
% an interpolated point, but the nearest xindex
% is always returned from the interp function below
% so that next/prev move function works correctly
set(H.xindex,'String','interp');
else
set(H.xindex,'String',num2str(H.data.xindex));
end
set(H.xindex,'Value',H.data.xindex);
tracestr = sprintf('%d',H.data.yindex);
set(H.yindex,'String',tracestr,'Value',uint16(H.data.yindex));
%set(H.trace,'Value',uint16(H.data.yindex));
% Create the crosshair lines on the figure, crossing at the x,y point
x_rng = get(H.axis,'Xlim');
y_rng = get(H.axis,'Ylim');
set(H.data.xline,'Xdata',[H.data.xpoint H.data.xpoint],'Ydata',y_rng);
set(H.data.yline,'Ydata',[H.data.ypoint H.data.ypoint],'Xdata',x_rng);
% Update the x,y values displayed for the x,y point
xstring = sprintf('%g',H.data.xpoint);
ystring = sprintf('%g',H.data.ypoint);
set(H.xvalue,'String',xstring,'Value',H.data.xpoint);
set(H.yvalue,'String',ystring,'Value',H.data.ypoint);
% Calculate the f(x) function
fyeq = get(H.fyeq,'string');
fyval = eval(strrep(fyeq,'y',ystring));
fystr = sprintf('%g',fyval);
set(H.fyvalue,'String',fystr,'Value',fyval);
% Calculate the f(y) function
fxeq = get(H.fxeq,'string');
fxval = eval(strrep(fxeq,'x',xstring));
fxstr = sprintf('%g',fxval);
set(H.fxvalue,'String',fxstr,'Value',fxval);
return
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [ H ] = interpY( H )
% In this function, xdata & ydata are arrays, not matrices
xdata = H.data.xdata(:,H.data.yindex);
ydata = H.data.ydata(:,H.data.yindex);
if H.data.xpoint >= max(xdata)
H.data.xpoint = max(xdata);
H.data.xindex = find(xdata == max(xdata));
H.data.ypoint = ydata(H.data.xindex);
return;
elseif H.data.xpoint <= min(xdata)
H.data.xpoint = min(xdata);
H.data.xindex = find(xdata == min(xdata));
H.data.ypoint = ydata(H.data.xindex);
return;
end
% 'none|nearest|linear|spline|cubic'
interp = get(H.interp,'Value');
switch interp
case 1
% Given that xdata & ydata are the same length arrays,
% we can find the ypoint given the nearest xpoint.
[H.data.xindex, H.data.xpoint] = NearestXYArrayPoint( xdata, H.data.xpoint );
H.data.ypoint = ydata(H.data.xindex);
case 2
H.data.ypoint = interp1( xdata, ydata, H.data.xpoint, 'nearest' );
case 3
H.data.ypoint = interp1( xdata, ydata, H.data.xpoint, 'linear' );
case 4
H.data.ypoint = interp1( xdata, ydata, H.data.xpoint, 'spline' );
case 5
H.data.ypoint = interp1( xdata, ydata, H.data.xpoint, 'cubic' );
otherwise
%use default (linear in matlabR12)
H.data.ypoint = interp1( xdata, ydata, H.data.xpoint );
end
return
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [ Yall ] = interpYall( H )
xdata = H.data.xdata(:,H.data.yindex);
Yall = H.data.ydata;
if H.data.xpoint >= max(xdata),
H.data.xpoint = max(xdata);
H.data.xindex = find(xdata == max(xdata));
Yall = ydata(:,H.data.xindex);
return;
elseif H.data.xpoint <= min(xdata),
H.data.xpoint = min(xdata);
H.data.xindex = find(xdata == min(xdata));
Yall = ydata(:,H.data.xindex);
return;
end
% 'none|nearest|linear|spline|cubic'
interp = get(H.interp,'Value');
switch interp,
case 1
% do nothing in this case
case 2
Yall = interp1( xdata, Yall, H.data.xpoint, 'nearest' );
case 3
Yall = interp1( xdata, Yall, H.data.xpoint, 'linear' );
case 4
Yall = interp1( xdata, Yall, H.data.xpoint, 'spline' );
case 5
Yall = interp1( xdata, Yall, H.data.xpoint, 'cubic' );
otherwise
%use default (linear in matlabR12)
Yall = interp1( xdata, Yall, H.data.xpoint );
end
return
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [ index, point ] = NearestXYArrayPoint( data_array, point, type )
if ~exist('type','var') type = ''; end
% In this function, input data_array is an array, not a matrix.
% This function returns the data point in the array
% that has the closest value to the value given (point). In
% the context of 'crosshair' the point is a mouse position.
if point >= max(data_array)
point = max(data_array);
index = find(data_array == point);
return;
elseif point <= min(data_array)
point = min(data_array);
index = find(data_array == point);
return;
end
data_sorted = sort(data_array);
greater = find(data_sorted > point);
greater_index = greater(1);
lesser = find(data_sorted < point);
lesser_index = lesser(length(lesser));
greater_dif = data_sorted(greater_index) - point;
lesser_dif = point - data_sorted(lesser_index);
if strcmp(type,'nextx'),
index = greater_index;
elseif strcmp(type,'prevx'),
index = lesser_index;
else
if (greater_dif < lesser_dif)
index = find(data_array == data_sorted(greater_index));
else
index = find(data_array == data_sorted(lesser_index));
end
end
point = data_array(index);
return
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [ xpoint, xindex, ypoint, yindex ] = NearestXYMatrixPoint( Xdata, Ydata, xpoint, ypoint )
% In this function, Xdata & Ydata are matrices of the same dimensions.
% This function attempts to find the nearest values in Xdata & Ydata
% to the mouse position (xpoint, ypoint).
% It is assumed that Xdata has identical columns, so we only really
% need the first column to find the nearest value to xpoint.
[ xindex, xpoint ] = NearestXYArrayPoint( Xdata(:,1), xpoint );
% Now, given the xpoint, we can select just that row of the
% Ydata matrix corresponding to the xpoint.
ydata = Ydata(xindex,:);
% The ydata array is searched in same manner as the xdata
% array for the nearest value.
[ yindex, ypoint ] = NearestXYArrayPoint( ydata, ypoint );
return
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [ data ] = get_data,
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Get Line Data from Current Axes
if gca,
% see if we have stored any data in userdata already
data = get(gca,'userdata');
if isfield(data,'lineobj'),
if data.lineobj,
return;
end
end
data.ylim = get(gca,'ylim');
% Get all the line objects from the current axis
data.lineobj = findobj(gca,'Type','line');
if data.lineobj,
data.lines = length(data.lineobj);
data.xdata = [];
data.ydata = [];
data.xpoint = [];
data.ypoint = [];
data.xindex = 1;
data.yindex = 1;
for i = data.lines:-1:1,
% put line data into a columns of data
data.xdata(:,i) = get(data.lineobj(i),'XData').';
data.ydata(:,i) = get(data.lineobj(i),'YData').';
end
% Set X,Y cross hair lines
% Do this after finding all the line axis children
% to avoid confusing these lines with those of the
% plot itself (counted above).
x_rng = get(gca,'Xlim');
y_rng = get(gca,'Ylim');
data.xline = line(x_rng,[y_rng(1) y_rng(1)]);
data.yline = line(x_rng,[y_rng(1) y_rng(1)]);
set(data.xline,'Color','r','EraseMode','xor','tag','XHR_XLINE');
set(data.yline,'Color','r','EraseMode','xor','tag','XHR_YLINE');
set(gca,'userdata',data);
else
fprintf('No lines in current axes\n');
end
end
return
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [H] = INIT
H.gui = gcf; % Get current figure handles
H.axis = gca; % Get current axis handles
H.data = get_data;
% store current button fcn
H.button = get(H.gui,'WindowButtonDownFcn');
% set XHR button down fcn
set(H.gui,'WindowButtonDownFcn','crosshair_subplots(''down'');');
set(H.gui,'KeyPressFcn','crosshair_subplots(''keypress'');');
% Match background figure colour
bgcolor = get(H.gui,'Color');
% Try to adapt the foreground colour a little
black = find(bgcolor <= .6);
fgcolor = [0 0 0]; %black text
if length(black)>2, fgcolor = [1 1 1]; end
Font.FontName = 'Helvetica';
Font.FontUnits = 'Pixels';
Font.FontSize = 10;
Font.FontWeight = 'normal';
Font.FontAngle = 'normal';
if findobj(gcf,'tag','XHR_YFLIP'),
H.yflip = findobj(gcf,'tag','XHR_YFLIP');
else
H.yflip = uicontrol(H.gui,'tag','XHR_YFLIP','Style','pushbutton',...
'Units','Normalized',Font,...
'Position',[.00 .95 .08 .05],...
'TooltipString','Flip Y Axis', ...
'String','Flip',...
'Callback',strcat('XHR = get(gcf,''userdata'');',...
'ydir = get(XHR.axis,''YDir'');',...
'if isequal(ydir,''normal''),',...
' set(XHR.axis,''YDir'',''reverse'');',...
'else,',...
' set(XHR.axis,''YDir'',''normal'');',...
'end;',...
'set(XHR.gui,''userdata'',XHR); figure(XHR.gui); clear XHR;'));
end
if findobj(gcf,'tag','XHR_YRESET'),
H.yreset = findobj(gcf,'tag','XHR_YRESET');
else
H.yreset = uicontrol(H.gui,'tag','XHR_YRESET','Style','pushbutton',...
'Units','Normalized',Font,...
'Position',[.00 .90 .08 .05],...
'TooltipString','Reset Axis Limits', ...
'String','Reset',...
'Callback',strcat('XHR = get(gcf,''userdata'');',...
'XHR.data.ylim = get(XHR.axis,''ylim'');',...
'set(XHR.ymin,''string'',sprintf(''%g'',XHR.data.ylim(1)));',...
'set(XHR.ymax,''string'',sprintf(''%g'',XHR.data.ylim(2)));',...
'set(XHR.gui,''userdata'',XHR); figure(XHR.gui); clear XHR;'));
end
if findobj(gcf,'tag','XHR_YMIN'),
H.ymin = findobj(gcf,'tag','XHR_YMIN');
else
H.ymin = uicontrol(H.gui,'tag','XHR_YMIN','Style','edit',...
'Units','Normalized',Font,...
'Position',[.00 .85 .08 .05],...
'HorizontalAlign','left',...
'TooltipString','Set Y min', ...
'String',sprintf('%g',H.data.ylim(1)),...
'Callback',strcat('XHR = get(gcf,''userdata'');',...
'ymin = str2num(get(XHR.ymin,''string''));',...
'XHR.data.ylim(1) = ymin;',...
'set(XHR.axis,''ylim'',XHR.data.ylim);',...
'set(XHR.ymin,''string'',sprintf(''%g'',XHR.data.ylim(1)));',...
'set(XHR.gui,''userdata'',XHR); figure(XHR.gui); clear XHR ymin;'));
end
if findobj(gcf,'tag','XHR_YMAX'),
H.ymax = findobj(gcf,'tag','XHR_YMAX');
else
H.ymax = uicontrol(H.gui,'tag','XHR_YMAX','Style','edit',...
'Units','Normalized',Font,...
'Position',[.00 .80 .08 .05],...
'HorizontalAlign','left',...
'TooltipString','Set Y max', ...
'String',sprintf('%g',H.data.ylim(2)),...
'Callback',strcat('XHR = get(gcf,''userdata'');',...
'ymax = str2num(get(XHR.ymax,''string''));',...
'XHR.data.ylim(2) = ymax;',...
'set(XHR.axis,''ylim'',XHR.data.ylim);',...
'set(XHR.ymax,''string'',sprintf(''%g'',XHR.data.ylim(2)));',...
'set(XHR.gui,''userdata'',XHR); figure(XHR.gui); clear XHR ymax;'));
end
if findobj(gcf,'tag','XHR_GRID'),
H.grid = findobj(gcf,'tag','XHR_GRID');
else
H.grid = uicontrol(H.gui,'tag','XHR_GRID','Style','checkbox',...
'Units','Normalized',Font,...
'Position',[.00 .75 .08 .05],...
'BackgroundColor',bgcolor,'ForegroundColor',fgcolor,...
'TooltipString','Toggle plot grid on/off.', ...
'String','grid',...
'Callback',strcat('XHR = get(gcf,''userdata'');',...
'grid(XHR.axis);',...
'set(XHR.gui,''userdata'',XHR); figure(XHR.gui); clear XHR;'));
end
if findobj(gcf,'tag','XHR_XVALUE'),
H.xvalue = findobj(gcf,'tag','XHR_XVALUE');
else
H.xvalue = uicontrol(H.gui,'tag','XHR_XVALUE','Style','edit',...
'Units','Normalized',Font,...
'Position',[.13 .95 .15 .05],...
'BackGroundColor',[ 0 .9 0],'ForeGroundColor',[ 0 0 0],...
'TooltipString','X value (Read Only)',...
'String',' ');
end
if findobj(gcf,'tag','XHR_YVALUE'),
H.yvalue = findobj(gcf,'tag','XHR_YVALUE');
else
H.yvalue = uicontrol(H.gui,'tag','XHR_YVALUE','Style','edit',...
'Units','Normalized',Font,...
'Position',[.28 .95 .15 .05],...
'BackGroundColor',[ 0 0 .9],'ForeGroundColor',[ 1 1 1],...
'TooltipString','Y value (Read Only)',...
'String',' ');
end
if findobj(gcf,'tag','XHR_FXEQ'),
H.fxeq = findobj(gcf,'tag','XHR_FXEQ');
else
H.fxeq = uicontrol(H.gui,'tag','XHR_FXEQ','Style','edit',...
'Units','Normalized',...
'Position',[.45 .95 .10 .05],...
'TooltipString','Define f(x) equation here',...
'BackGroundColor',[ 0 .9 0],...
'ForeGroundColor',[ 0 0 0],'String','x');
end
if findobj(gcf,'tag','XHR_FXVALUE'),
H.fxvalue = findobj(gcf,'tag','XHR_FXVALUE');
else
H.fxvalue = uicontrol(H.gui,'tag','XHR_FXVALUE','Style','edit',...
'Units','Normalized',...
'Position',[.55 .95 .15 .05],...
'TooltipString','f(x) result',...
'BackGroundColor',[ 0 .9 0],...
'ForeGroundColor',[ 0 0 0],'String',' ');
end
if findobj(gcf,'tag','XHR_FYEQ'),
H.fyeq = findobj(gcf,'tag','XHR_FYEQ');
else
H.fyeq = uicontrol(H.gui,'tag','XHR_FYEQ','Style','edit',...
'Units','Normalized',...
'Position',[.70 .95 .10 .05],...
'TooltipString','Define f(y) equation here',...
'BackGroundColor',[ 0 0 .9],...
'ForeGroundColor',[ 1 1 1],'String','y');
end
if findobj(gcf,'tag','XHR_FYVALUE'),
H.fyvalue = findobj(gcf,'tag','XHR_FYVALUE');
else
H.fyvalue = uicontrol(H.gui,'tag','XHR_FYVALUE','Style','edit',...
'Units','Normalized',...
'Position',[.80 .95 .15 .05],...
'TooltipString','f(y) result',...
'BackGroundColor',[ 0 0 .9],...
'ForeGroundColor',[ 1 1 1],'String',' ');
end
if findobj(gcf,'tag','XHR_YINDEX'),
H.yindex = findobj(gcf,'tag','XHR_YINDEX');
else
H.yindex = uicontrol(H.gui,'tag','XHR_YINDEX','Style','edit',...
'Units','Normalized',Font,...
'Position',[.92 .87 .08 .05],...
'BackGroundColor',[ 0 0 .9],'ForeGroundColor',[ 1 1 1],...
'TooltipString','Enter Y index into plot data matrix. Same as trace number.',...
'String','1','Value',1,...
'Callback',strcat('XHR = get(gcf,''userdata'');',...
'yi = str2num(get(XHR.yindex,''String''));',...
'XHR.data.yindex = yi;',...
'set(XHR.gui,''userdata'',XHR); clear XHR yi; ',...
'[Xpoint,Ypoint] = crosshair_subplots(''changey'');'));
end
if findobj(gcf,'tag','XHR_YPREV'),
H.yprev = findobj(gcf,'tag','XHR_YPREV');
else
H.yprev = uicontrol(H.gui,'tag','XHR_YPREV','Style','Push',...
'Units','Normalized',Font,...
'Position',[.92 .82 .04 .05],...
'String','<',...
'TooltipString','Goto Previous Y Index (channel).',...
'CallBack','[Xpoint,Ypoint] = crosshair_subplots(''prevy'');');
end
if findobj(gcf,'tag','XHR_YNEXT'),
H.ynext = findobj(gcf,'tag','XHR_YNEXT');
else
H.ynext = uicontrol(H.gui,'tag','XHR_YNEXT','Style','Push',...
'Units','Normalized',Font,...
'Position',[.96 .82 .04 .05],...
'String','>',...
'TooltipString','Goto Next Y Index (channel).',...
'CallBack','[Xpoint,Ypoint] = crosshair_subplots(''nexty'');');
end
if findobj(gcf,'tag','XHR_YLT'),
H.yLT = findobj(gcf,'tag','XHR_YLT');
else
H.yLT = uicontrol(H.gui,'tag','XHR_YLT','Style','Push',...
'Units','Normalized',Font,...
'Position',[.92 .77 .04 .05],...
'String','LT',...
'TooltipString','Goto next Y Less Than current Y.',...
'CallBack','[Xpoint,Ypoint] = crosshair_subplots(''yLT'');');
end
if findobj(gcf,'tag','XHR_YGT'),
H.yGT = findobj(gcf,'tag','XHR_YGT');
else
H.yGT = uicontrol(H.gui,'tag','XHR_YGT','Style','Push',...
'Units','Normalized',Font,...
'Position',[.96 .77 .04 .05],...
'String','GT',...
'TooltipString','Goto next Y Greater Than current Y.',...
'CallBack','[Xpoint,Ypoint] = crosshair_subplots(''yGT'');');
end
if findobj(gcf,'tag','XHR_XINDEX'),
H.xindex = findobj(gcf,'tag','XHR_XINDEX');
else
H.xindex = uicontrol(H.gui,'tag','XHR_XINDEX','Style','edit',...
'Units','Normalized',Font,...
'Position',[.92 .70 .08 .05],...
'BackGroundColor',[ 0 .9 0],'ForeGroundColor',[ 0 0 0],...
'TooltipString','Enter X index into plot data matrix. Only available for interpolation = ''none''.',...
'String','1','Value',1,...
'Callback',strcat('XHR = get(gcf,''userdata'');',...
'xi = str2num(get(XHR.xindex,''String''));',...
'XHR.data.xindex = xi;',...
'set(XHR.xinterp,''value'',0); ',...
'set(XHR.xinterp,''string'',''0''); ',...
'set(XHR.interp, ''value'',1); ',...
'set(XHR.gui,''userdata'',XHR); clear XHR; ',...
'[Xpoint,Ypoint] = crosshair_subplots(''changex'');'));
end
if findobj(gcf,'tag','XHR_XPREV'),
H.xprev = findobj(gcf,'tag','XHR_XPREV');
else
H.xprev = uicontrol(H.gui,'tag','XHR_XPREV','Style','Push',...
'Units','Normalized',Font,...
'Position',[.92 .65 .04 .05],...
'String','<',...
'TooltipString','Goto Previous X Index (no interpolation).',...
'CallBack','[Xpoint,Ypoint] = crosshair_subplots(''prevx'');');
end
if findobj(gcf,'tag','XHR_XNEXT'),
H.xnext = findobj(gcf,'tag','XHR_XNEXT');
else
H.xnext = uicontrol(H.gui,'tag','XHR_XNEXT','Style','Push',...
'Units','Normalized',Font,...
'Position',[.96 .65 .04 .05],...
'String','>',...
'TooltipString','Goto Next X Index (no interpolation).',...
'CallBack','[Xpoint,Ypoint] = crosshair_subplots(''nextx'');');