0001 function D = prefix_images(D, action, prefix, flags)
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019 def_flags = struct('check_exist', 'none', ...
0020 'check_all', 1, ...
0021 'check_swap', 0);
0022
0023 if nargin < 2
0024 action = 'remove';
0025 end
0026 if nargin < 3
0027 prefix = 's';
0028 end
0029 if nargin < 4
0030 flags = [];
0031 end
0032 flags = mars_struct('ffillsplit', def_flags, flags);
0033 if flags.check_swap, flags.check_exist = 'error'; end
0034
0035
0036 if ~has_images(D)
0037 warning('Design does not contain images');
0038 return
0039 end
0040 VY = get_images(D);
0041
0042
0043 files = strvcat(VY(:).fname);
0044 fpaths = spm_str_manip(files, 'h');
0045 fns = spm_str_manip(files, 't');
0046 nf = size(files, 1);
0047
0048 switch lower(action)
0049 case 'remove'
0050 s_is = strmatch(prefix, fns);
0051 if length(s_is) == nf
0052 fns(:, 1:length(prefix)) = [];
0053 else
0054 warning(['Not all analysis files prefixed with ''' prefix ...
0055 ''', design has not been changed'])
0056 return
0057 end
0058 case 'add'
0059 fns = [repmat(prefix, nf, 1) fns];
0060 otherwise
0061 error(['Warped action ' action]);
0062 end
0063
0064 newfns = cellstr(strcat(fpaths, filesep, fns));
0065 [VY(:).fname] = deal(newfns{:});
0066
0067
0068 c_e = lower(flags.check_exist);
0069 switch c_e
0070 case 'none'
0071 case {'error', 'warn'}
0072 if ~flags.check_all, cV = VY(1); else cV = VY; end
0073 e_f = 1;
0074 n_chk = prod(size(cV));
0075 for v = 1:n_chk
0076 if ~exist(newfns{v}, 'file'), e_f = 0; break; end
0077 end
0078 if ~e_f
0079 str = sprintf('Image %s does not exist', newfns{v});
0080 if strcmp(c_e, 'error'), error(str); else warn(str); end
0081 end
0082 otherwise
0083 error(sprintf('Who asked for %s?', c_e));
0084 end
0085 if flags.check_swap
0086 if flags.check_all
0087 for v = 1:nf
0088 if mars_vol_utils('is_swapped_wrong', VY(v))
0089 VY(v) = mars_vol_utils('byte_swap', VY(v));
0090 end
0091 end
0092 else
0093 if mars_vol_utils('is_swapped_wrong', VY(1))
0094 VY = mars_vol_utils('byte_swap', VY);
0095 end
0096 end
0097 end
0098
0099 D = set_images(D, VY);
0100