function gen_rev_spm_mat(from_slice,to_slice)
%  GEN_REV_SPM_MAT Generate SPM matrix
%  GEN_REV_SPM_MAT(from_slice,to_slice) generates an .m file for the from_slice
%  analyze file to get transformed into to_slice coordinates.
%  Needed from_slice.hdr, from_slice.mtf, to_slice.hdr,
%  to_slice.mtf files. MTF-files are genereted by braintools convert-tools.
%  The voxelsize of the headerfiles must be correct.
%  NOERRORCORRECTION whatsoever
%  This is the version for reversed blocks created by mriWorkshop
%  flip y and flip s enabled
  
  
% Files to read  
  from_mtf=strcat(from_slice,'.mtf')
  from_hdr=strcat(from_slice,'.hdr')
  to_mtf=strcat(to_slice,'.mtf')
  to_hdr=strcat(to_slice,'.hdr')
  
% Get data from 
  f=fopen(from_mtf)
  from_ext=fscanf(f,'Automatic generated MTF (Auto reversed Col/Norm)%f%f')
  from_anf=fscanf(f,'%f',3)*1000
  from_mat=fscanf(f,'%f',[3,3])
  from_mat=from_mat'
  from_sli=fscanf(f,'%f',3)*1000
  fclose(f)

  [from_dim from_vox from_scale from_type from_offset ...
   from_descrip]=spm_hread(from_hdr)

% from_anf points to 1st slice origin is in the middle
  from_origin=from_anf+from_sli*(from_dim(3)-1)/2
  from_trans=[from_mat from_origin;0 0 0 1]
  

% normal matrix (which would be assumed without .mat file)
  from_nmat=[diag(from_vox),-(diag(from_dim'+1)/2)*from_vox';0 0 0 1]
  
% Get data to  
  f=fopen(to_mtf)
  to_ext=fscanf(f,'Automatic generated MTF (Auto reversed Col/Norm)%f%f')
  to_anf=fscanf(f,'%f',3)*1000
  to_mat=fscanf(f,'%f',[3,3])
  to_mat=to_mat'
  to_sli=fscanf(f,'%f',3)*1000
  fclose(f)

  [to_dim to_vox to_scale to_type to_offset to_descrip]= ...
      spm_hread(to_hdr)
  
  % to_anf points to 1st slice origin is in the middle
  to_origin=to_anf+to_sli*(to_dim(3)-1)/2
  to_trans=[to_mat to_origin;0 0 0 1]

  M=to_trans^-1*from_trans*from_nmat
  save(from_slice)