


compatibility function to allow spm_get calls with SPM5
SPM5 uses a function called spm_select to do file selection instead of
the spm_get of versions 96-2. This breaks a lot of old code; here we
wrap the most common calls to spm_get so that we can use
spm_select. I've only wrapped the spm_get calls used in marsbar.
Usually, file / directory selection call is of format:
FORMAT P = spm_get(N, ext, prompt)
Input
N - matrix specifying what (file or dir) to select, and how
many to select.
ext - the filter for files to select
prompt - the prompt to display for the selection window
Output
P - a string matrix of file names
First argument can also be action string:
FORMAT cpath = spm_get('CPath',path,cwd)
(returns canonical version of file path 'path')
FORMAT [files,dirs]=spm_select('files',direc,filt)
(Returns files matching the filter (filt) and directories within dir)
See spm_select from the spm5 distribution, and spm_get from spm2
distribution
$Id$


0001 function varargout = spm_get(Action, varargin) 0002 % compatibility function to allow spm_get calls with SPM5 0003 % 0004 % SPM5 uses a function called spm_select to do file selection instead of 0005 % the spm_get of versions 96-2. This breaks a lot of old code; here we 0006 % wrap the most common calls to spm_get so that we can use 0007 % spm_select. I've only wrapped the spm_get calls used in marsbar. 0008 % 0009 % Usually, file / directory selection call is of format: 0010 % FORMAT P = spm_get(N, ext, prompt) 0011 % Input 0012 % N - matrix specifying what (file or dir) to select, and how 0013 % many to select. 0014 % ext - the filter for files to select 0015 % prompt - the prompt to display for the selection window 0016 % 0017 % Output 0018 % P - a string matrix of file names 0019 % 0020 % First argument can also be action string: 0021 % FORMAT cpath = spm_get('CPath',path,cwd) 0022 % (returns canonical version of file path 'path') 0023 % FORMAT [files,dirs]=spm_select('files',direc,filt) 0024 % (Returns files matching the filter (filt) and directories within dir) 0025 % 0026 % See spm_select from the spm5 distribution, and spm_get from spm2 0027 % distribution 0028 % 0029 % $Id$ 0030 0031 nout = max(nargout,1); 0032 0033 if nargin < 1 0034 Action=Inf; 0035 end 0036 0037 % If the first argument is a string, this is an action 0038 if ischar(Action) 0039 switch(lower(Action)) 0040 case 'cpath' 0041 varargout = {spm_select('cpath', varargin{:})}; 0042 case 'files' 0043 if nargin < 2 0044 Dir = pwd; 0045 else 0046 Dir = varargin{1}; 0047 end 0048 if nargin < 3 0049 Filt = '.*'; 0050 else 0051 Filt = sf_get_to_select_filt(varargin{2}); 0052 end 0053 varargout = {spm_select('list', Dir, Filt)}; 0054 % The old spm_get returned full file paths 0055 Files = varargout{1}; 0056 varargout{1} = [repmat([Dir filesep], size(Files, 1), 1) Files]; 0057 otherwise 0058 error([Action ': I''m sorry, but I can''t do that']); 0059 end 0060 if strcmp(Action, 'files'), Action='List'; end 0061 0062 return 0063 end 0064 0065 % Otherwise, must be file / directory selection 0066 if nargin < 2 0067 Filt = 'any'; 0068 else 0069 Filt = varargin{1}; 0070 varargin(1) = []; 0071 Filt = sf_get_to_select_filt(Filt); 0072 end 0073 if any(Action < 0) 0074 % Directory select 0075 Action = abs(Action); 0076 Filt = 'dir'; 0077 end 0078 if nargin<3 0079 Prompt='Select files...'; 0080 else 0081 Prompt = varargin{1}; 0082 varargin(1) = []; 0083 end 0084 varargout = {spm_select(Action, Filt, Prompt, varargin{:})}; 0085 if isempty(varargout), return, end 0086 % Cell array prompt should return cell array of arguments 0087 if iscellstr(Prompt) 0088 if isempty(varargout{1}) 0089 varargout{1} = {}; 0090 else 0091 varargout{1} = cellstr(varargout{1}); 0092 end 0093 end 0094 return 0095 0096 0097 % Subfunctions 0098 function F = sf_get_to_select_filt(F) 0099 % Converts filter for old spm_get routine to something for spm_select 0100 if strcmpi(F, 'image'), F = lower(F); return, end 0101 F = sf_shexp_regexp(F); 0102 return 0103 0104 function new_str = sf_shexp_regexp(old_str) 0105 % Does basic conversion from shell expression to regexp 0106 % Have ignored some quoting issues here: 0107 % http://www.unix.org.ua/orelly/perl/cookbook/ch06_10.htm 0108 % sub glob2pat { 0109 % my $globstr = shift; 0110 % my %patmap = ( 0111 % '*' => '.*', 0112 % '?' => '.', 0113 % '[' => '[', 0114 % ']' => ']', 0115 % ); 0116 % $globstr =~ s{(.)} { $patmap{$1} || "\Q$1" }ge; 0117 % return '^' . $globstr . '$'; 0118 %} 0119 0120 new_str = '^'; 0121 for c = old_str 0122 switch c 0123 case '*' 0124 nc = '.*'; 0125 case '?' 0126 nc = '.'; 0127 case {'.', '^', '$', '+'} 0128 nc = ['\' c]; 0129 otherwise 0130 nc = c; 0131 end 0132 new_str = [new_str nc]; 0133 end 0134 new_str = [new_str '$']; 0135 return 0136