-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspm_input.m
2387 lines (2137 loc) · 87.7 KB
/
spm_input.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 varargout = spm_input(varargin)
% Comprehensive graphical and command line input function
% FORMATs (given in Programmers Help)
%_______________________________________________________________________
%
% spm_input handles most forms of interactive user input for SPM.
% (File selection is handled by spm_select.m)
%
% There are five types of input: String, Evaluated, Conditions, Buttons
% and Menus: These prompt for string input; string input which is
% evaluated to give a numerical result; selection of one item from a
% set of buttons; selection of an item from a menu.
%
% - STRING, EVALUATED & CONDITION input -
% For STRING, EVALUATED and CONDITION input types, a prompt is
% displayed adjacent to an editable text entry widget (with a lilac
% background!). Clicking in the entry widget allows editing, pressing
% <RETURN> or <ENTER> enters the result. You must enter something,
% empty answers are not accepted. A default response may be pre-specified
% in the entry widget, which will then be outlined. Clicking the border
% accepts the default value.
%
% Basic editing of the entry widget is supported *without* clicking in
% the widget, provided no other graphics widget has the focus. (If a
% widget has the focus, it is shown highlighted with a thin coloured
% line. Clicking on the window background returns the focus to the
% window, enabling keyboard accelerators.). This enables you to type
% responses to a sequence of questions without having to repeatedly
% click the mouse in the text widgets. Supported are BackSpace and
% Delete, line kill (^U). Other standard ASCII characters are appended
% to the text in the entry widget. Press <RETURN> or <ENTER> to submit
% your response.
%
% A ContextMenu is provided (in the figure background) giving access to
% relevant utilities including the facility to load input from a file
% (see spm_load.m and examples given below): Click the right button on
% the figure background.
%
% For EVALUATED input, the string submitted is evaluated in the base
% MatLab workspace (see MatLab's `eval` command) to give a numerical
% value. This permits the entry of numerics, matrices, expressions,
% functions or workspace variables. I.e.:
% i) - a number, vector or matrix e.g. "[1 2 3 4]"
% "[1:4]"
% "1:4"
% ii) - an expression e.g. "pi^2"
% "exp(-[1:36]/5.321)"
% iii) - a function (that will be invoked) e.g. "spm_load('tmp.dat')"
% (function must be on MATLABPATH) "input_cov(36,5.321)"
% iv) - a variable from the base workspace
% e.g. "tmp"
%
% The last three options provide a great deal of power: spm_load will
% load a matrix from an ASCII data file and return the results. When
% called without an argument, spm_load will pop up a file selection
% dialog. Alternatively, this facility can be gained from the
% ContextMenu. The second example assummes a custom funcion called
% input_cov has been written which expects two arguments, for example
% the following file saved as input_cov.m somewhere on the MATLABPATH
% (~/matlab, the matlab subdirectory of your home area, and the current
% directory, are on the MATLABPATH by default):
%
% function [x] = input_cov(n,decay)
% % data input routine - mono-exponential covariate
% % FORMAT [x] = input_cov(n,decay)
% % n - number of time points
% % decay - decay constant
% x = exp(-[1:n]/decay);
%
% Although this example is trivial, specifying large vectors of
% empirical data (e.g. reaction times for 72 scans) is efficient and
% reliable using this device. In the last option, a variable called tmp
% is picked up from the base workspace. To use this method, set the
% variables in the MatLab base workspace before starting an SPM
% procedure (but after starting the SPM interface). E.g.
% >> tmp=exp(-[1:36]/5.321)
%
% Occasionally a vector of a specific length will be required: This
% will be indicated in the prompt, which will start with "[#]", where
% # is the length of vector(s) required. (If a matrix is entered then
% at least one dimension should equal #.)
%
% Occasionally a specific type of number will be required. This should
% be obvious from the context. If you enter a number of the wrong type,
% you'll be alerted and asked to re-specify. The types are i) Real
% numbers; ii) Integers; iii) Whole numbers [0,1,2,3,...] & iv) Natural
% numbers [1,2,3,...]
%
% CONDITIONS type input is for getting indicator vectors. The features
% of evaluated input described above are complimented as follows:
% v) - a compressed list of digits 0-9 e.g. "12121212"
% ii) - a list of indicator characters e.g. "abababab"
% a-z mapped to 1-26 in alphabetical order, *except* r ("rest")
% which is mapped to zero (case insensitive, [A:Z,a:z] only)
% ...in addition the response is checked to ensure integer condition indices.
% Occasionally a specific number of conditions will be required: This
% will be indicated in the prompt, which will end with (#), where # is
% the number of conditions required.
%
% CONTRAST type input is for getting contrast weight vectors. Enter
% contrasts as row-vectors. Contrast weight vectors will be padded with
% zeros to the correct length, and checked for validity. (Valid
% contrasts are estimable, which are those whose weights vector is in
% the row-space of the design matrix.)
%
% Errors in string evaluation for EVALUATED & CONDITION types are
% handled gracefully, the user notified, and prompted to re-enter.
%
% - BUTTON input -
% For Button input, the prompt is displayed adjacent to a small row of
% buttons. Press the approprate button. The default button (if
% available) has a dark outline. Keyboard accelerators are available
% (provided no graphics widget has the focus): <RETURN> or <ENTER>
% selects the default button (if available). Typing the first character
% of the button label (case insensitive) "presses" that button. (If
% these Keys are not unique, then the integer keys 1,2,... "press" the
% appropriate button.)
%
% The CommandLine variant presents a simple menu of buttons and prompts
% for a selection. Any default response is indicated, and accepted if
% an empty line is input.
%
%
% - MENU input -
% For Menu input, the prompt is displayed in a pull down menu widget.
% Using the mouse, a selection is made by pulling down the widget and
% releasing the mouse on the appropriate response. The default response
% (if set) is marked with an asterisk. Keyboard accelerators are
% available (provided no graphic widget has the focus) as follows: 'f',
% 'n' or 'd' move forward to next response down; 'b', 'p' or 'u' move
% backwards to the previous response up the list; the number keys jump
% to the appropriate response number; <RETURN> or <ENTER> slelects the
% currently displayed response. If a default is available, then
% pressing <RETURN> or <ENTER> when the prompt is displayed jumps to
% the default response.
%
% The CommandLine variant presents a simple menu and prompts for a selection.
% Any default response is indicated, and accepted if an empty line is
% input.
%
%
% - Combination BUTTON/EDIT input -
% In this usage, you will be presented with a set of buttons and an
% editable text widget. Click one of the buttons to choose that option,
% or type your response in the edit widget. Any default response will
% be shown in the edit widget. The edit widget behaves in the same way
% as with the STRING/EVALUATED input, and expects a single number.
% Keypresses edit the text widget (rather than "press" the buttons)
% (provided no other graphics widget has the focus). A default response
% can be selected with the mouse by clicking the thick border of the
% edit widget.
%
%
% - Command line -
% If YPos is 0 or global CMDLINE is true, then the command line is used.
% Negative YPos overrides CMDLINE, ensuring the GUI is used, at
% YPos=abs(YPos). Similarly relative YPos beginning with '!'
% (E.g.YPos='!+1') ensures the GUI is used.
%
% spm_input uses the SPM 'Interactive' window, which is 'Tag'ged
% 'Interactive'. If there is no such window, then the current figure is
% used, or an 'Interactive' window created if no windows are open.
%
%-----------------------------------------------------------------------
% Programers help is contained in the main body of spm_input.m
%-----------------------------------------------------------------------
% See : input.m (MatLab Reference Guide)
% See also : spm_select.m (SPM file selector dialog)
% : spm_input.m (Input wrapper function - handles batch mode)
%_______________________________________________________________________
% Copyright (C) 2008 Wellcome Trust Centre for Neuroimaging
% Andrew Holmes
% $Id: spm_input.m 7564 2019-04-02 10:50:42Z guillaume $
%=======================================================================
% - FORMAT specifications for programers
%=======================================================================
% generic - [p,YPos] = spm_input(Prompt,YPos,Type,...)
% string - [p,YPos] = spm_input(Prompt,YPos,'s',DefStr)
% string+ - [p,YPos] = spm_input(Prompt,YPos,'s+',DefStr)
% evaluated - [p,YPos] = spm_input(Prompt,YPos,'e',DefStr,n)
% - natural - [p,YPos] = spm_input(Prompt,YPos,'n',DefStr,n,mx)
% - whole - [p,YPos] = spm_input(Prompt,YPos,'w',DefStr,n,mx)
% - integer - [p,YPos] = spm_input(Prompt,YPos,'i',DefStr,n)
% - real - [p,YPos] = spm_input(Prompt,YPos,'r',DefStr,n,mm)
% condition - [p,YPos] = spm_input(Prompt,YPos,'c',DefStr,n,m)
% contrast - [p,YPos] = spm_input(Prompt,YPos,'x',DefStr,n,X)
% permutation- [p,YPos] = spm_input(Prompt,YPos,'p',DefStr,P,n)
% button - [p,YPos] = spm_input(Prompt,YPos,'b',Labels,Values,DefItem)
% button/edit combo's (edit for string or typed scalar evaluated input)
% [p,YPos] = spm_input(Prompt,YPos,'b?1',Labels,Values,DefStr,mx)
% ...where ? in b?1 specifies edit widget type as with string & eval'd input
% - [p,YPos] = spm_input(Prompt,YPos,'n1',DefStr,mx)
% - [p,YPos] = spm_input(Prompt,YPos,'w1',DefStr,mx)
% button dialog
% - [p,YPos] = spm_input(Prompt,YPos,'bd',...
% Labels,Values,DefItem,Title)
% menu - [p,YPos] = spm_input(Prompt,YPos,'m',Labels,Values,DefItem)
% display - spm_input(Message,YPos,'d',Label)
% display - (GUI only) spm_input(Alert,YPos,'d!',Label)
%
% yes/no - [p,YPos] = spm_input(Prompt,YPos,'y/n',Values,DefItem)
% buttons (shortcut) where Labels is a bar delimited string
% - [p,YPos] = spm_input(Prompt,YPos,Labels,Values,DefItem)
%
% NB: Natural numbers are [1:Inf), Whole numbers are [0:Inf)
%
% -- Parameters (input) --
%
% Prompt - prompt string
% - Defaults (missing or empty) to 'Enter an expression'
%
% YPos - (numeric) vertical position {1 - 12}
% - overriden by global CMDLINE
% - 0 for command line
% - negative to force GUI
% - (string) relative vertical position E.g. '+1'
% - relative to last position used
% - overriden by global CMDLINE
% - YPos(1)=='!' forces GUI E.g. '!+1'
% - '_' is a shortcut for the lowest GUI position
% - Defaults (missing or empty) to '+1'
%
% Type - type of interrogation
% - 's'tring
% - 's+' multi-line string
% - p returned as cellstr (nx1 cell array of strings)
% - DefStr can be a cellstr or string matrix
% - 'e'valuated string
% - 'n'atural numbers
% - 'w'hole numbers
% - 'i'ntegers
% - 'r'eals
% - 'c'ondition indicator vector
% - 'x' - contrast entry
% - If n(2) or design matrix X is specified, then
% contrast matrices are padded with zeros to have
% correct length.
% - if design matrix X is specified, then contrasts are
% checked for validity (i.e. in the row-space of X)
% (checking handled by spm_SpUtil)
% - 'b'uttons
% - 'bd' - button dialog: Uses MatLab's questdlg
% - For up to three buttons
% - Prompt can be a cellstr with a long multiline message
% - CmdLine support as with 'b' type
% - button/edit combo's: 'be1','bn1','bw1','bi1','br1'
% - second letter of b?1 specifies type for edit widget
% - 'n1' - single natural number (buttons 1,2,... & edit)
% - 'w1' - single whole number (buttons 0,1,... & edit)
% - 'm'enu pulldown
% - 'y/n' : Yes or No buttons
% (See shortcuts below)
% - bar delimited string : buttons with these labels
% (See shortcuts below)
% - Defaults (missing or empty) to 'e'
%
% DefStr - Default string to be placed in entry widget for string and
% evaluated types
% - Defaults to ''
%
% n ('e', 'c' & 'p' types)
% - Size of matrix requred
% - NaN for 'e' type implies no checking - returns input as evaluated
% - length of n(:) specifies dimension - elements specify size
% - Inf implies no restriction
% - Scalar n expanded to [n,1] (i.e. a column vector)
% (except 'x' contrast type when it's [n,np] for np
% - E.g: [n,1] & [1,n] (scalar n) prompt for an n-vector,
% returned as column or row vector respectively
% [1,Inf] & [Inf,1] prompt for a single vector,
% returned as column or row vector respectively
% [n,Inf] & [Inf,n] prompts for any number of n-vectors,
% returned with row/column dimension n respectively.
% [a,b] prompts for an 2D matrix with row dimension a and
% column dimension b
% [a,Inf,b] prompt for a 3D matrix with row dimension a,
% page dimension b, and any column dimension.
% - 'c' type can only deal with single vectors
% - NaN for 'c' type treated as Inf
% - Defaults (missing or empty) to NaN
%
% n ('x'type)
% - Number of contrasts required by 'x' type (n(1))
% ( n(2) can be used specify length of contrast vectors if )
% ( a design matrix isn't passed )
% - Defaults (missing or empty) to 1 - vector contrast
%
% mx ('n', 'w', 'n1', 'w1', 'bn1' & 'bw1' types)
% - Maximum value (inclusive)
%
% mm ('r' type)
% - Maximum and minimum values (inclusive)
%
% m - Number of unique conditions required by 'c' type
% - Inf implies no restriction
% - Defaults (missing or empty) to Inf - no restriction
%
% P - set (vector) of numbers of which a permutation is required
%
% X - Design matrix for contrast checking in 'x' type
% - Can be either a straight matrix or a space structure (see spm_sp)
% - Column dimension of design matrix specifies length of contrast
% vectors (overriding n(2) is specified).
%
% Title - Title for questdlg in 'bd' type
%
% Labels - Labels for button and menu types.
% - string matrix, one label per row
% - bar delimited string
% E.g. 'AnCova|Scaling|None'
%
% Values - Return values corresponding to Labels for button and menu types
% - j-th row is returned if button / menu item j is selected
% (row vectors are transposed)
% - Defaults (missing or empty) to - (button) Labels
% - ( menu ) menu item numbers
%
% DefItem - Default item number, for button and menu types.
%
% -- Parameters (output) --
% p - results
% YPos - Optional second output argument returns GUI position just used
%
%-----------------------------------------------------------------------
% WINDOWS:
%
% spm_input uses the SPM 'Interactive' 'Tag'ged window. If this isn't
% available and no figures are open, an 'Interactive' SPM window is
% created (`spm('CreateIntWin')`). If figures are available, then the
% current figure is used *unless* it is 'Tag'ged.
%
%-----------------------------------------------------------------------
% SHORTCUTS:
%
% Buttons SHORTCUT - If the Type parameter is a bar delimited string, then
% the Type is taken as 'b' with the specified labels, and the next parameter
% (if specified) is taken for the Values.
%
% Yes/No question shortcut - p = spm_input(Prompt,YPos,'y/n') expands
% to p = spm_input(Prompt,YPos,'b','yes|no',...), enabling easy use of
% spm_input for yes/no dialogue. Values defaults to 'yn', so 'y' or 'n'
% is returned as appropriate.
%
%-----------------------------------------------------------------------
% EXAMPLES:
% ( Specified YPos is overriden if global CMDLINE is )
% ( true, when the command line versions are used. )
%
% p = spm_input
% Command line input of an evaluated string, default prompt.
% p = spm_input('Enter a value',1)
% Evaluated string input, prompted by 'Enter a value', in
% position 1 of the dialog figure.
% p = spm_input(str,'+1','e',0.001)
% Evaluated string input, prompted by contents of string str,
% in next position of the dialog figure.
% Default value of 0.001 offered.
% p = spm_input(str,2,'e',[],5)
% Evaluated string input, prompted by contents of string str,
% in second position of the dialog figure.
% Vector of length 5 required - returned as column vector
% p = spm_input(str,2,'e',[],[Inf,5])
% ...as above, but can enter multiple 5-vectors in a matrix,
% returned with 5-vectors in rows
% p = spm_input(str,0,'c','ababab')
% Condition string input, prompted by contents of string str
% Uses command line interface.
% Default string of 'ababab' offered.
% p = spm_input(str,0,'c','010101')
% As above, but default string of '010101' offered.
% [p,YPos] = spm_input(str,'0','s','Image')
% String input, same position as last used, prompted by str,
% default of 'Image' offered. YPos returns GUI position used.
% p = spm_input(str,'-1','y/n')
% Yes/No buttons for question with prompt str, in position one
% before the last used Returns 'y' or 'n'.
% p = spm_input(str,'-1','y/n',[1,0],2)
% As above, but returns 1 for yes response, 0 for no,
% with 'no' as the default response
% p = spm_input(str,4,'AnCova|Scaling')
% Presents two buttons labelled 'AnCova' & 'Scaling', with
% prompt str, in position 4 of the dialog figure. Returns the
% string on the depresed button, where buttons can be pressed
% with the mouse or by the respective keyboard accelerators
% 'a' & 's' (or 'A' & 'S').
% p = spm_input(str,-4,'b','AnCova|Scaling',[],2)
% As above, but makes "Scaling" the default response, and
% overrides global CMDLINE
% p = spm_input(str,0,'b','AnCova|Scaling|None',[1,2,3])
% Prompts for [A]ncova / [S]caling / [N]one in MatLab command
% window, returns 1, 2, or 3 according to the first character
% of the entered string as one of 'a', 's', or 'n' (case
% insensitive).
% p = spm_input(str,1,'b','AnCova',1)
% Since there's only one button, this just displays the response
% in GUI position 1 (or on the command line if global CMDLINE
% is true), and returns 1.
% p = spm_input(str,'+0','br1','None|Mask',[-Inf,NaN],0.8)
% Presents two buttons labelled "None" & "Mask" (which return
% -Inf & NaN if clicked), together with an editable text widget
% for entry of a single real number. The default of 0.8 is
% initially presented in the edit window, and can be selected by
% pressing return.
% Uses the previous GUI position, unless global CMDLINE is true,
% in which case a command-line equivalent is used.
% p = spm_input(str,'+0','w1')
% Prompts for a single whole number using a combination of
% buttons and edit widget, using the previous GUI position,
% or the command line if global CMDLINE is true.
% p = spm_input(str,'!0','m','Single Subject|Multi Subject|Multi Study')
% Prints the prompt str in a pull down menu containing items
% 'Single Subject', 'Multi Subject' & 'Multi Study'. When OK is
% clicked p is returned as the index of the choice, 1,2, or 3
% respectively. Uses last used position in GUI, irrespective of
% global CMDLINE
% p = spm_input(str,5,'m',...
% 'Single Subject|Multi Subject|Multi Study',...
% ['SS';'MS';'SP'],2)
% As above, but returns strings 'SS', 'MS', or 'SP' according to
% the respective choice, with 'MS; as the default response.
% p = spm_input(str,0,'m',...
% 'Single Subject|Multi Subject|Multi Study',...
% ['SS';'MS';'SP'],2)
% As above, but the menu is presented in the command window
% as a numbered list.
% spm_input('AnCova, GrandMean scaling',0,'d')
% Displays message in a box in the MatLab command window
% [null,YPos]=spm_input('Session 1','+1','d!','fMRI')
% Displays 'fMRI: Session 1' in next GUI position of the
% 'Interactive' window. If CMDLINE is 1, then nothing is done.
% Position used is returned in YPos.
%
%-----------------------------------------------------------------------
% FORMAT h = spm_input(Prompt,YPos,'m!',Labels,cb,UD,XCB);
% GUI PullDown menu utility - creates a pulldown menu in the Interactive window
% FORMAT H = spm_input(Prompt,YPos,'b!',Labels,cb,UD,XCB);
% GUI Buttons utility - creates GUI buttons in the Interactive window
%
% Prompt, YPos, Labels - as with 'm'enu/'b'utton types
% cb - CallBack string
% UD - UserData
% XCB - Extended CallBack handling - allows different CallBack for each item,
% and use of UD in CallBack strings. [Defaults to 1 for PullDown type
% when multiple CallBacks specified, 0 o/w.]
% H - Handle of 'PullDown' uicontrol / 'Button's
%
% In "normal" mode (when XCB is false), this is essentially a utility
% to create a PullDown menu widget or set of buttons in the SPM
% 'Interactive' figure, using positioning and Label definition
% conveniences of the spm_input 'm'enu & 'b'utton types. If Prompt is
% not empty, then the PullDown/Buttons appears on the right, with the
% Prompt on the left, otherwise the PullDown/Buttons use the whole
% width of the Interactive figure. The PopUp's CallBack string is
% specified in cb, and [optional] UserData may be passed as UD.
%
% For buttons, a separate callback can be specified for each button, by
% passing the callbacks corresponding to the Labels as rows of a
% cellstr or string matrix.
%
% This "different CallBacks" facility can also be extended to the
% PullDown type, using the "extended callback" mode (when XCB is
% true). % In addition, in "extended callback", you can use UD to
% refer to the UserData argument in the CallBack strings. (What happens
% is this: The cb & UD are stored as fields in the PopUp's UserData
% structure, and the PopUp's callback is set to spm_input('!m_cb'),
% which reads UD into the functions workspace and eval's the
% appropriate CallBack string. Note that this means that base
% workspace variables are inaccessible (put what you need in UD), and
% that any return arguments from CallBack functions are not passed back
% to the base workspace).
%
%
%-----------------------------------------------------------------------
% UTILITY FUNCTIONS:
%
% FORMAT colour = spm_input('!Colour')
% Returns colour for input widgets, as specified in COLOUR parameter at
% start of code.
% colour - [r,g,b] colour triple
%
% FORMAT [iCond,msg] = spm_input('!iCond',str,n,m)
% Parser for special 'c'ondition type: Handles digit strings and
% strings of indicator chars.
% str - input string
% n - length of condition vector required [defaut Inf - no restriction]
% m - number of conditions required [default Inf - no restrictions]
% iCond - Integer condition indicator vector
% msg - status message
%
% FORMAT hM = spm_input('!InptConMen',Finter,H)
% Sets a basic Input ContextMenu for the figure
% Finter - figure to set menu in
% H - handles of objects to delete on "crash out" option
% hM - handle of UIContextMenu
%
% FORMAT [CmdLine,YPos] = spm_input('!CmdLine',YPos)
% Sorts out whether to use CmdLine or not & canonicalises YPos
% CmdLine - Binary flag
% YPos - Position index
%
% FORMAT Finter = spm_input('!GetWin',F)
% Locates (or creates) figure to work in
% F - Interactive Figure, defaults to 'Interactive'
% Finter - Handle of figure to use
%
% FORMAT [PLoc,cF] = spm_input('!PointerJump',RRec,F,XDisp)
% Raise window & jump pointer over question
% RRec - Response rectangle of current question
% F - Interactive Figure, Defaults to 'Interactive'
% XDisp - X-displacement of cursor relative to RRec
% PLoc - Pointer location before jumping
% cF - Current figure before making F current.
%
% FORMAT [PLoc,cF] = spm_input('!PointerJumpBack',PLoc,cF)
% Replace pointer and reset CurrentFigure back
% PLoc - Pointer location before jumping
% cF - Previous current figure
%
% FORMAT spm_input('!PrntPrmpt',Prompt,TipStr,Title)
% Print prompt for CmdLine questioning
% Prompt - prompt string, callstr, or string matrix
% TipStr - tip string
% Title - title string
%
% FORMAT [Frec,QRec,PRec,RRec] = spm_input('!InputRects',YPos,rec,F)
% Returns rectangles (pixels) used in GUI
% YPos - Position index
% rec - Rectangle specifier: String, one of 'Frec','QRec','PRec','RRec'
% Defaults to '', which returns them all.
% F - Interactive Figure, Defaults to spm_figure('FindWin','Interactive')
% FRec - Position of interactive window
% QRec - Position of entire question
% PRec - Position of prompt
% RRec - Position of response
%
% FORMAT spm_input('!DeleteInputObj',F)
% Deltes input objects (only) from figure F
% F - Interactive Figure, Defaults to spm_figure('FindWin','Interactive')
%
% FORMAT [CPos,hCPos] = spm_input('!CurrentPos',F)
% Returns currently used GUI question positions & their handles
% F - Interactive Figure, Defaults to spm_figure('FindWin','Interactive')
% CPos - Vector of position indices
% hCPos - (n x CPos) matrix of object handles
%
% FORMAT h = spm_input('!FindInputObj',F)
% Returns handles of input GUI objects in figure F
% F - Interactive Figure, Defaults to spm_figure('FindWin','Interactive')
% h - vector of object handles
%
% FORMAT [NPos,CPos,hCPos] = spm_input('!NextPos',YPos,F,CmdLine)
% Returns next position index, specified by YPos
% YPos - Absolute (integer) or relative (string) position index
% Defaults to '+1'
% F - Interactive Figure, defaults to spm_figure('FindWin','Interactive')
% CmdLine - Command line? Defaults to spm_input('!CmdLine',YPos)
% NPos - Next position index
% CPos & hCPos - as for !CurrentPos
%
% FORMAT NPos = spm_input('!SetNextPos',YPos,F,CmdLine)
% Sets up for input at next position index, specified by YPos. This utility
% function can be used stand-alone to implicitly set the next position
% by clearing positions NPos and greater.
% YPos - Absolute (integer) or relative (string) position index
% Defaults to '+1'
% F - Interactive Figure, defaults to spm_figure('FindWin','Interactive')
% CmdLine - Command line? Defaults to spm_input('!CmdLine',YPos)
% NPos - Next position index
%
% FORMAT MPos = spm_input('!MaxPos',F,FRec3)
% Returns maximum position index for figure F
% F - Interactive Figure, Defaults to spm_figure('FindWin','Interactive')
% Not required if FRec3 is specified
% FRec3 - Length of interactive figure in pixels
%
% FORMAT spm_input('!EditableKeyPressFcn',h,ch)
% KeyPress callback for GUI string / eval input
%
% FORMAT spm_input('!ButtonKeyPressFcn',h,Keys,DefItem,ch)
% KeyPress callback for GUI buttons
%
% FORMAT spm_input('!PullDownKeyPressFcn',h,ch,DefItem)
% KeyPress callback for GUI pulldown menus
%
% FORMAT spm_input('!m_cb')
% Extended CallBack handler for 'p' PullDown utility type
%
% FORMAT spm_input('!dScroll',h,str)
% Scroll text string in object h
% h - handle of text object
% Prompt - Text to scroll (Defaults to 'UserData' of h)
%
%-----------------------------------------------------------------------
% SUBFUNCTIONS:
%
% FORMAT [Keys,Labs] = sf_labkeys(Labels)
% Make unique character keys for the Labels, ignoring case.
% Used with 'b'utton types.
%
% FORMAT [p,msg] = sf_eEval(str,Type,n,m)
% Common code for evaluating various input types.
%
% FORMAT str = sf_SzStr(n,l)
% Common code to construct prompt strings for pre-specified vector/matrix sizes
%
% FORMAT [p,msg] = sf_SzChk(p,n,msg)
% Common code to check (& canonicalise) sizes of input vectors/matrices
%
%_______________________________________________________________________
% @(#)spm_input.m 2.8 Andrew Holmes 03/03/04
%-Parameters
%=======================================================================
PJump = 1; %-Jumping of pointer to question?
TTips = 1; %-Use ToolTipStrings? (which can be annoying!)
ConCrash = 1; %-Add "crash out" option to 'Interactive'fig.ContextMenu
%-Condition arguments
%=======================================================================
if nargin<1||isempty(varargin{1}), Prompt=''; else Prompt=varargin{1}; end
if ~isempty(Prompt) && ischar(Prompt) && Prompt(1)=='!'
%-Utility functions have Prompt string starting with '!'
Type = Prompt;
else %-Should be an input request: get Type & YPos
if nargin<3||isempty(varargin{3}), Type='e'; else Type=varargin{3}; end
if any(Type=='|'), Type='b|'; end
if nargin<2||isempty(varargin{2}), YPos='+1'; else YPos=varargin{2}; end
[CmdLine,YPos] = spm_input('!CmdLine',YPos);
if ~CmdLine %-Setup for GUI use
%-Locate (or create) figure to work in
Finter = spm_input('!GetWin');
COLOUR = get(Finter,'Color');
%-Find out which Y-position to use, setup for use
YPos = spm_input('!SetNextPos',YPos,Finter,CmdLine);
%-Determine position of objects
[FRec,QRec,PRec,RRec]=spm_input('!InputRects',YPos,'',Finter);
end
end
switch lower(Type)
case {'s','s+','e','n','w','i','r','c','x','p'} %-String and evaluated input
%=======================================================================
%-Condition arguments
if nargin<6||isempty(varargin{6}), m=[]; else m=varargin{6}; end
if nargin<5||isempty(varargin{5}), n=[]; else n=varargin{5}; end
if nargin<4, DefStr=''; else DefStr=varargin{4}; end
if strcmpi(Type,'s+')
%-DefStr should be a cellstr for 's+' type.
if isempty(DefStr), DefStr = {};
else DefStr = cellstr(DefStr); end
DefStr = DefStr(:);
else
%-DefStr needs to be a string
if ~ischar(DefStr), DefStr=num2str(DefStr); end
DefStr = DefStr(:)';
end
strM='';
switch lower(Type) %-Type specific defaults/setup
case 's', TTstr='enter string';
case 's+',TTstr='enter string - multi-line';
case 'e', TTstr='enter expression to evaluate';
case 'n', TTstr='enter expression - natural number(s)';
if ~isempty(m), strM=sprintf(' (in [1,%d])',m); TTstr=[TTstr,strM]; end
case 'w', TTstr='enter expression - whole number(s)';
if ~isempty(m), strM=sprintf(' (in [0,%d])',m); TTstr=[TTstr,strM]; end
case 'i', TTstr='enter expression - integer(s)';
case 'r', TTstr='enter expression - real number(s)';
if ~isempty(m), TTstr=[TTstr,sprintf(' in [%g,%g]',min(m),max(m))]; end
case 'c', TTstr='enter indicator vector e.g. 0101... or abab...';
if ~isempty(m) && isfinite(m), strM=sprintf(' (%d)',m); end
case 'x', TTstr='enter contrast matrix';
case 'p',
if isempty(n), error('permutation of what?'), else P=n(:)'; end
if isempty(m), n = [1,length(P)]; end
m = P;
if isempty(setxor(m,[1:max(m)]))
TTstr=['enter permutation of [1:',num2str(max(m)),']'];
else
TTstr=['enter permutation of [',num2str(m),']'];
end
otherwise
TTstr='enter expression';
end
strN = sf_SzStr(n);
if CmdLine %-Use CmdLine to get answer
%-----------------------------------------------------------------------
spm_input('!PrntPrmpt',[Prompt,strN,strM],TTstr)
%-Do Eval Types in Base workspace, catch errors
switch lower(Type), case 's'
if ~isempty(DefStr)
Prompt=[Prompt,' (Default: ',DefStr,' )'];
end
str = input([Prompt,' : '],'s');
if isempty(str), str=DefStr; end
while isempty(str)
spm('Beep')
fprintf('! %s : enter something!\n',mfilename)
str = input([Prompt,' : '],'s');
if isempty(str), str=DefStr; end
end
p = str; msg = '';
case 's+'
fprintf(['Multi-line input: Type ''.'' on a line',...
' of its own to terminate input.\n'])
if ~isempty(DefStr)
fprintf('Default : (press return to accept)\n')
fprintf(' : %s\n',DefStr{:})
end
fprintf('\n')
str = input('l001 : ','s');
while (isempty(str) || strcmp(str,'.')) && isempty(DefStr)
spm('Beep')
fprintf('! %s : enter something!\n',mfilename)
str = input('l001 : ','s');
end
if isempty(str)
%-Accept default
p = DefStr;
else
%-Got some input, allow entry of additional lines
p = {str};
str = input(sprintf('l%03u : ',length(p)+1),'s');
while ~strcmp(str,'.')
p = [p;{str}];
str = input(sprintf('l%03u : ',length(p)+1),'s');
end
end
msg = '';
otherwise
if ~isempty(DefStr)
Prompt=[Prompt,' (Default: ',DefStr,' )'];
end
str = input([Prompt,' : '],'s');
if isempty(str), str=DefStr; end
[p,msg] = sf_eEval(str,Type,n,m);
while ischar(p)
spm('Beep'), fprintf('! %s : %s\n',mfilename,msg)
str = input([Prompt,' : '],'s');
if isempty(str), str=DefStr; end
[p,msg] = sf_eEval(str,Type,n,m);
end
end
if ~isempty(msg), fprintf('\t%s\n',msg), end
else %-Use GUI to get answer
%-----------------------------------------------------------------------
%-Create text and edit control objects
%---------------------------------------------------------------
hPrmpt = uicontrol(Finter,'Style','Text',...
'String',[strN,Prompt,strM],...
'Tag',['GUIinput_',int2str(YPos)],...
'UserData','',...
'BackgroundColor',COLOUR,...
'HorizontalAlignment','Right',...
'Position',PRec);
if TTips, set(hPrmpt,'ToolTipString',[strN,Prompt,strM]); end
%-Default button surrounding edit widget (if a DefStr given)
%-Callback sets hPrmpt UserData, and EditWidget string, to DefStr
% (Buttons UserData holds handles [hPrmpt,hEditWidget], set later)
cb = ['set(subsref(get(gcbo,''UserData''),substruct(''()'',{1})),',...
'''UserData'',get(gcbo,''String'')),',...
'set(subsref(get(gcbo,''UserData''),substruct(''()'',{2})),',...
'''String'',get(gcbo,''String''))'];
if ~isempty(DefStr)
if iscellstr(DefStr), str=[DefStr{1},'...'];
else str=DefStr; end
hDef = uicontrol(Finter,'Style','PushButton',...
'String',DefStr,...
'ToolTipString',...
['Click on border to accept default: ' str],...
'Tag',['GUIinput_',int2str(YPos)],...
'UserData',[],...
'BackgroundColor',COLOUR,...
'CallBack',cb,...
'Position',RRec+[-2,-2,+4,+4]);
else
hDef = [];
end
%-Edit widget: Callback puts string into hPrompts UserData
cb = 'set(get(gcbo,''UserData''),''UserData'',get(gcbo,''String''))';
h = uicontrol(Finter,'Style','Edit',...
'String',DefStr,...
'Max',strcmpi(Type,'s+')+1,...
'Tag',['GUIinput_',int2str(YPos)],...
'UserData',hPrmpt,...
'CallBack',cb,...
'Horizontalalignment','Left',...
'BackgroundColor','w',...
'Position',RRec);
set(hDef,'UserData',[hPrmpt,h])
uifocus(h);
if TTips, set(h,'ToolTipString',TTstr), end
%-Figure ContextMenu for shortcuts
hM = spm_input('!InptConMen',Finter,[hPrmpt,hDef,h]);
cb = [ 'set(get(gcbo,''UserData''),''String'',',...
'[''spm_load('''''',spm_select(1),'''''')'']), ',...
'set(get(get(gcbo,''UserData''),''UserData''),''UserData'',',...
'get(get(gcbo,''UserData''),''String''))'];
uimenu(hM,'Label','load from text file','Separator','on',...
'CallBack',cb,'UserData',h)
%-Bring window to fore & jump pointer to edit widget
[PLoc,cF] = spm_input('!PointerJump',RRec,Finter);
%-Setup FigureKeyPressFcn for editing of entry widget without clicking
set(Finter,'KeyPressFcn',[...
'spm_input(''!EditableKeyPressFcn'',',...
'findobj(gcf,''Tag'',''GUIinput_',int2str(YPos),''',',...
'''Style'',''edit''),',...
'get(gcbf,''CurrentCharacter''))'])
%-Wait for edit, do eval Types in Base workspace, catch errors
%---------------------------------------------------------------
waitfor(hPrmpt,'UserData')
if ~ishandle(hPrmpt), error(['Input window cleared whilst waiting ',...
'for response: Bailing out!']), end
str = get(hPrmpt,'UserData');
switch lower(Type), case 's'
p = str; msg = '';
case 's+'
p = cellstr(str); msg = '';
otherwise
[p,msg] = sf_eEval(str,Type,n,m);
while ischar(p)
set(h,'Style','Text',...
'String',msg,'HorizontalAlignment','Center',...
'ForegroundColor','r')
spm('Beep'), pause(2)
set(h,'Style','Edit',...
'String',str,...
'HorizontalAlignment','Left',...
'ForegroundColor','k')
%set(hPrmpt,'UserData','');
waitfor(hPrmpt,'UserData')
if ~ishandle(hPrmpt), error(['Input window cleared ',...
'whilst waiting for response: Bailing out!']),end
str = get(hPrmpt,'UserData');
[p,msg] = sf_eEval(str,Type,n,m);
end
end
%-Fix edit window, clean up, reposition pointer, set CurrentFig back
delete([hM,hDef]), set(Finter,'KeyPressFcn','')
set(h,'Style','Text','HorizontalAlignment','Center',...
'ToolTipString',msg,...
'BackgroundColor',COLOUR)
spm_input('!PointerJumpBack',PLoc,cF)
drawnow
end % (if CmdLine)
%-Return response
%-----------------------------------------------------------------------
varargout = {p,YPos};
case {'b','bd','b|','y/n','be1','bn1','bw1','bi1','br1',...
'-n1','n1','-w1','w1','m'} %-'b'utton & 'm'enu Types
%=======================================================================
%-Condition arguments
switch lower(Type), case {'b','be1','bi1','br1','m'}
m = []; Title = '';
if nargin<6, DefItem=[]; else DefItem=varargin{6}; end
if nargin<5, Values=[]; else Values =varargin{5}; end
if nargin<4, Labels=''; else Labels =varargin{4}; end
case 'bd'
if nargin<7, Title=''; else Title =varargin{7}; end
if nargin<6, DefItem=[]; else DefItem=varargin{6}; end
if nargin<5, Values=[]; else Values =varargin{5}; end
if nargin<4, Labels=''; else Labels =varargin{4}; end
case 'y/n'
Title = '';
if nargin<5, DefItem=[]; else DefItem=varargin{5}; end
if nargin<4, Values=[]; else Values =varargin{4}; end
if isempty(Values), Values='yn'; end
Labels = {'yes','no'};
case 'b|'
Title = '';
if nargin<5, DefItem=[]; else DefItem=varargin{5}; end
if nargin<4, Values=[]; else Values =varargin{4}; end
Labels = varargin{3};
case 'bn1'
if nargin<7, m=[]; else m=varargin{7}; end
if nargin<6, DefItem=[]; else DefItem=varargin{6}; end
if nargin<5, Values=[]; else Values =varargin{5}; end
if nargin<4, Labels=[1:5]'; Values=[1:5]; Type='-n1';
else Labels=varargin{4}; end
case 'bw1'
if nargin<7, m=[]; else m=varargin{7}; end
if nargin<6, DefItem=[]; else DefItem=varargin{6}; end
if nargin<5, Values=[]; else Values =varargin{5}; end
if nargin<4, Labels=[0:4]'; Values=[0:4]; Type='-w1';
else Labels=varargin{4}; end
case {'-n1','n1','-w1','w1'}
if nargin<5, m=[]; else m=varargin{5}; end
if nargin<4, DefItem=[]; else DefItem=varargin{4}; end
switch lower(Type)
case {'n1','-n1'}, Labels=[1:min([5,m])]'; Values=Labels'; Type='-n1';
case {'w1','-w1'}, Labels=[0:min([4,m])]'; Values=Labels'; Type='-w1';
end
end
%-Check some labels were specified
if isempty(Labels), error('No Labels specified'), end
if iscellstr(Labels), Labels=char(Labels); end
%-Convert Labels "option" string to string matrix if required
if ischar(Labels) && any(Labels(:)=='|')
OptStr=Labels;
BarPos=find([OptStr=='|',1]);
Labels=OptStr(1:BarPos(1)-1);
for Bar = 2:sum(OptStr=='|')+1
Labels=strvcat(Labels,OptStr(BarPos(Bar-1)+1:BarPos(Bar)-1));
end
end
%-Set default Values for the Labels
if isempty(Values)
if strcmpi(Type,'m')
Values=[1:size(Labels,1)]';
else
Values=Labels;
end
else
%-Make sure Values are in rows
if size(Labels,1)>1 && size(Values,1)==1, Values = Values'; end
%-Check numbers of Labels and Values match
if (size(Labels,1)~=size(Values,1))
error('Labels & Values incompatible sizes'), end
end
%-Numeric Labels to strings
if isnumeric(Labels)
tmp = Labels; Labels = cell(size(tmp,1),1);
for i=1:numel(tmp), Labels{i}=num2str(tmp(i,:)); end
Labels=char(Labels);
end
switch lower(Type), case {'b','bd','b|','y/n'} %-Process button types
%=======================================================================
%-Make unique character keys for the Labels, sort DefItem
%---------------------------------------------------------------
nLabels = size(Labels,1);
[Keys,Labs] = sf_labkeys(Labels);
if ~isempty(DefItem) && any(DefItem==[1:nLabels])
DefKey = Keys(DefItem);
else
DefItem = 0;
DefKey = '';
end
if CmdLine
%-Display question prompt
spm_input('!PrntPrmpt',Prompt,'',Title)
%-Build prompt
%-------------------------------------------------------
if ~isempty(Labs)
Prmpt = ['[',Keys(1),']',deblank(Labs(1,:)),' '];
for i = 2:nLabels
Prmpt=[Prmpt,'/ [',Keys(i),']',deblank(Labs(i,:)),' '];
end
else
Prmpt = ['[',Keys(1),'] '];
for i = 2:nLabels, Prmpt=[Prmpt,'/ [',Keys(i),'] ']; end
end