clear;clc;
cd /home/subhadip/Autism_AnatNet/austistic_study

maindirpath=['/home/subhadip/Autism_AnatNet/austistic_study/'];
%specify the subject directories
subjdir=dir('aHU*');

for l=1%:length(chk)
   
   temppath=strcat(maindirpath,subjdir(l).name,'/','dti*');
  
   datafiles=dir(temppath);
   
   for m=1:length(datafiles)
   
   %split the individual datafiles
   datafile=strcat(maindirpath,subjdir(l).name,'/',datafiles(m).name);
   datapath=strcat(maindirpath,subjdir(l).name,'/');
   [status currdir] = system([' fslsplit ' datafile ]);
   
   %move the splitted volumes to the subject directory
   tempvolfiles=strcat(maindirpath,'vol*');
   movefile(tempvolfiles , datapath);
   
   
   %merge the bo and other volfiles
   setenv('FSLOUTPUTTYPE','NIFTI_GZ');
   
   tempvolpath=strcat(datapath , 'vol*');
   tempfiles=dir(tempvolpath);
   
   b0file=strcat(datapath , 'b0.nii.gz');
   
   ch=num2str(m,'%02d');
   outputname=strcat(datapath,'dataset',ch);
   
   tempchk=strcat( outputname, ' ' , b0file)
   
   a=['fslmerge  -t ' tempchk];
   for l=2:length(tempfiles)
       file=strcat( datapath,tempfiles(l).name);
       a = strcat(a , ' ', file)
   end

   [status currdir] = system([a])
   
   
%    ch=num2str(m,'%02d');
%    outputname=strcat(datapath,'dataset',ch);

   
%    [status currdir] = system([' rm ' datapath ]);
   volfiles=strcat(subjdir(l),'/','vol*.nii.gz');
   [status currdir] = system([' rm ' volfiles ]);
   
   end

end