%Reverse normalize a set of coordinates (.tal) with a specified sn3d.mat file
%Sue Whitfield 2001

% multiple-ROI/param loops added by Jeff 12/2002


coor_array = spm_get(Inf,'.tal',' Select TAL coord input filename(s):');

sn_array = spm_get(Inf,'_sn3d.mat',' Select normalization parameter set(s):');

outprepend = spm_input('Select string to prepend to filename(s)', 1, 's')  
resdir = spm_get(-1,'',...
			'Select directory to save output file(s).');
					
for i = 1:size(coor_array,1)  % number of rois files to be transformed
    coor = deblank(coor_array(i,:));
    fprintf('\n\nApplying parameters to ROI:\n');
    fprintf([coor '\n\n']);

    for j = 1:size(sn_array,1)  % number of normalization parameter sets
    sn = deblank(sn_array(j,:));
    fprintf('Applying the following parameters:\n');
    fprintf([sn '\n']);  
    
    XYZ = load(coor);
    [oXYZ] = obj_coords(sn, XYZ);
    org = oXYZ; 
    cd(resdir)
    [coor_dir, coor_name] = fileparts(coor);
    outname = fullfile(resdir, [outprepend '_' coor_name '_' num2str(j) '.tal']);
    fid = fopen(outname, 'w');
    fprintf(fid, ' %g %g %g\n', org);  
    fclose(fid);
    fprintf('A new file was created:\n');
    fprintf([outname '\n']);
         
    end
end
