% mask_files.m    ajs 10-09-02

% =========================================================================



curr_dir = pwd ;



% choose files to mask



FILE_LIST = spm_get(Inf,'.img','Select Analyze Images to Multiply by a Mask:', curr_dir);

n       = size(FILE_LIST,1);





MASK = spm_get(1,'*.img','Select Analyze Binary Mask Image:', curr_dir);



% ===================================================================

% [pth filnam xt temp  ]= fileparts(P) ;



%   Perform algebraic functions on images

%   FORMAT Q = spm_imcalc_ui(P,Q,f,flags,Xtra_vars...)

%   P             - matrix of input image filenames

%                   [user prompted to select files if arg missing or empty]

%   Q             - name of output image

%                   [user prompted to enter filename if arg missing or empty]

%   f             - expression to be evaluated

%                   [user prompted to enter expression if arg missing or empty]

%   flags         - cell vector of flags: {dmtx,mask,type,hold}

%   dmtx          - Read images into data matrix?

%                   [defaults (missing or empty) to 0 - no]

%   mask          - implicit zero mask?

%                   [defaults (missing or empty) to 0]

%   type          - data type for output image (see spm_type)

%                   [defaults (missing or empty) to 4 - 16 bit signed shorts]

%   hold          - interpolation hold (see spm_slice_vol)

%                   [defaults (missing or empty) to 0 - nearest neighbour]

%   Xtra_vars...  - additional variables which can be used in expression

%   Q (output)    - full pathname of image written

%   Vo            - structure containing information on output image (see spm_vol)

%

   % original model  --- spm_imcalc_ui(P,'GM_048','i1./(i1+i2+eps).*i3')

   % newer model for cleaning up grey matter:    i1.*i4./(i1+i2+eps)

   % str = ['spm_imcalc_ui(P,''',fn3,'''',','' i1.*i4./(i1+i2+eps)''',')'   ]

   % eval(str)



for file_num = 1:n



 FILE = deblank(FILE_LIST(file_num,:));

 FILE_new = [FILE(1:end-4), '_msk']



 FILES = cell(2,1)



 FILES{1} = FILE ;

 FILES{2} = MASK ;

  P= char(FILES);



%  FORMAT Q = spm_imcalc_ui(P,Q,f,flags,Xtra_vars...)

 str = ['spm_imcalc_ui(P,''',FILE_new,'''',','' i1.*i2''',')'   ]

 eval(str)



   disp(['Masking  ',FILE, '   ',  num2str(file_num), '  of   ' num2str(n)]);



end
