
% This program generates .tal files listing the XYZ coordinates for the selelcted images.
function f = roi_list(varargin)

if nargin==0
    imgs = spm_vol(spm_get(Inf,'*.img','Select ROI images'));
else
    imgs = spm_vol(varargin{1});
end

for  i = 1:length(imgs)

img = imgs(i);

[Y,XYZ] = spm_read_vols(img);

XYZmm = XYZ(:,find(Y==max(max(max(Y)))));

[dir,name_tal] = fileparts(img.fname);

f_out{i,1} = char([dir,'/',name_tal,'.tal']);
%name_tal_full = [dir,'/',name_tal,'.tal'];


    fid = fopen(f_out{i,1},'w');
	     if fid == -1, error(['Unable to create new file - please check permissions', ...
								  ' in current directory.']); 
		  end
     fprintf(fid,'%g %g %g\n', XYZmm);
     fclose(fid);

     fprintf('\n\n----------------------------------------------');
     fprintf('\nA new file ''%s'' was created in %s',char([name_tal,'.tal']), char(dir));
     %fprintf('\n%s\n',pwd);

end

f = char(f_out);