Home > marsbar > spm5 > spm_get.m

spm_get

PURPOSE ^

compatibility function to allow spm_get calls with SPM5

SYNOPSIS ^

function varargout = spm_get(Action, varargin)

DESCRIPTION ^

 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, newwdir)
 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
 newwdir  - new working directory

 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

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SUBFUNCTIONS ^

SOURCE CODE ^

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, newwdir)
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 % newwdir  - new working directory
0017 %
0018 % Output
0019 % P        - a string matrix of file names
0020 %
0021 % First argument can also be action string:
0022 % FORMAT cpath = spm_get('CPath',path,cwd)
0023 % (returns canonical version of file path 'path')
0024 % FORMAT [files,dirs]=spm_select('files',direc,filt)
0025 % (Returns files matching the filter (filt) and directories within dir)
0026 %
0027 % See spm_select from the spm5 distribution, and spm_get from spm2
0028 % distribution
0029 
0030 nout = max(nargout,1);
0031 
0032 if nargin < 1
0033   Action=Inf;
0034 end
0035 
0036 % If the first argument is a string, this is an action
0037 if ischar(Action)
0038   switch(lower(Action))
0039    case 'cpath'
0040     varargout = {spm_select('cpath', varargin{:})};
0041    case 'files'
0042     if nargin < 2
0043       Dir = pwd;
0044     else
0045       Dir = varargin{1};
0046     end
0047     if nargin < 3
0048       Filt = '.*';
0049     else
0050       Filt = sf_get_to_select_filt(varargin{2});
0051     end
0052     varargout = {spm_select('list', Dir, Filt)};
0053     % The old spm_get returned full file paths
0054     Files = varargout{1};
0055     varargout{1} = [repmat([Dir filesep], size(Files, 1), 1) Files];
0056    otherwise
0057     error([Action ': I''m sorry, but I can''t do that']);
0058   end
0059   if strcmp(Action, 'files'), Action='List'; end
0060 
0061   return
0062 end
0063 
0064 % Otherwise, must be file / directory selection
0065 if nargin < 2
0066   Filt = 'any';
0067 else
0068   Filt = varargin{1};
0069   varargin(1) = [];
0070   Filt = sf_get_to_select_filt(Filt);
0071 end
0072 if any(Action < 0)
0073   % Directory select
0074   Action = abs(Action);
0075   Filt = 'dir';
0076 end
0077 if nargin<3
0078   Prompt='Select files...';
0079 else
0080   Prompt = varargin{1};
0081   varargin(1) = [];
0082 end
0083 if nargin<4
0084     wd = pwd;
0085 else
0086     wd = varargin{1};
0087     varargin(1) = []; % pop processed argument
0088 end
0089 if length(varargin) ~= 0
0090     error('Sorry, we do not handle this call to spm_select');
0091 end
0092 varargout = {spm_select(Action, Filt, Prompt, {''}, wd)};
0093 if isempty(varargout), return, end
0094 % Cell array prompt should return cell array of arguments
0095 if iscellstr(Prompt)
0096   if isempty(varargout{1})
0097     varargout{1} = {};
0098   else
0099     varargout{1} = cellstr(varargout{1});
0100   end
0101 end
0102 return
0103 
0104 
0105 % Subfunctions
0106 function F = sf_get_to_select_filt(F)
0107 % Converts filter for old spm_get routine to something for spm_select
0108 if strcmpi(F, 'image'), F = lower(F); return, end
0109 F = sf_shexp_regexp(F);
0110 return
0111 
0112 function new_str = sf_shexp_regexp(old_str)
0113 % Does basic conversion from shell expression to regexp
0114 % Have ignored some quoting issues here:
0115 % http://www.unix.org.ua/orelly/perl/cookbook/ch06_10.htm
0116 % sub glob2pat {
0117 %    my $globstr = shift;
0118 %    my %patmap = (
0119 %        '*' => '.*',
0120 %        '?' => '.',
0121 %        '[' => '[',
0122 %        ']' => ']',
0123 %    );
0124 %    $globstr =~ s{(.)} { $patmap{$1} || "\Q$1" }ge;
0125 %    return '^' . $globstr . '$';
0126 %}
0127 
0128 new_str = '^';
0129 for c = old_str
0130   switch c
0131    case '*'
0132     nc = '.*';
0133    case '?'
0134     nc = '.';
0135    case {'.', '^', '$', '+'}
0136     nc = ['\' c]; 
0137    otherwise
0138     nc = c;
0139   end
0140   new_str = [new_str nc];
0141 end
0142 new_str = [new_str '$'];
0143 return
0144   

Generated on Wed 11-May-2022 16:26:09 by m2html © 2003-2019