Home > marsbar > @mardo > prefix_images.m

prefix_images

PURPOSE ^

method for adding or removing prefix from file names in design

SYNOPSIS ^

function D = prefix_images(D, action, prefix, flags)

DESCRIPTION ^

 method for adding or removing prefix from file names in design
 FORMAT D = prefix_images(D, action, prefix)

 D          - mardo design
 action     - one of 'add' or 'remove'
 prefix     - prefix to remove
 flags      - optional struct containing none or more of fields
              'check_exist' - one of 'warn', 'error', 'none'
                      If not 'none' checks images exist with new
                      filenames
              'check_all'   - if not 0, checks all images, instead of
                              the first image in the image list
              'check_swap'  - if not 0, checks if images with new
                      filenames need byte swapping, and swaps if so
             
 $Id$

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function D = prefix_images(D, action, prefix, flags)
0002 % method for adding or removing prefix from file names in design
0003 % FORMAT D = prefix_images(D, action, prefix)
0004 %
0005 % D          - mardo design
0006 % action     - one of 'add' or 'remove'
0007 % prefix     - prefix to remove
0008 % flags      - optional struct containing none or more of fields
0009 %              'check_exist' - one of 'warn', 'error', 'none'
0010 %                      If not 'none' checks images exist with new
0011 %                      filenames
0012 %              'check_all'   - if not 0, checks all images, instead of
0013 %                              the first image in the image list
0014 %              'check_swap'  - if not 0, checks if images with new
0015 %                      filenames need byte swapping, and swaps if so
0016 %
0017 % $Id$
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 % get images
0036 if ~has_images(D)
0037   warning('Design does not contain images');
0038   return
0039 end
0040 VY = get_images(D);
0041 
0042 % remove prefix
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 % Do checks if necessary
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 % just check the first and apply to all
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 

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