


%Script to dump voxels in the glass brain into a txt file to be read into the Tal deamon


%outname = spm_input('Select TAL coord output filename ', 1, 's')  
%resdir = spm_get(-1,'',...
		%	'Select directory to save output file.');



a=SPM.XYZmm';
b=mni2tal(a);
n=size(b,1);
m=n/10000;
m=ceil(m);
P=SPM.title;
q=findstr(P,' ');
for i=1:length(q)
   P(q(i))='_';
end


%cd(resdir)

for j=1:m
%outfile=cat(2,P,'_k',num2str(SPM.k),'_T',num2str(SPM.u),'_',num2str(j),'.txt')
outfile=cat(2,P,'_k',num2str(SPM.k),'_T',num2str(SPM.u),'.txt')
fid=fopen(outfile,'a');
for i=(j-1)*10000+1:j*10000
   if i>n
      break
   else
      c=round(b(i,:));
	   fprintf(fid,'%2.0f	%2.0f	%2.0f\n',c);
   end   
   end
end
status=fclose(fid);
clear a b n P q c




