Skip to content

AFNI/NIfTI Server

Sections
Personal tools
You are here: Home » AFNI » Documentation

Doxygen Source Code Documentation


writeSPMmat.m

Go to the documentation of this file.
00001 function [err] = writeSPMmat (matin, matout)
00002 %
00003 %   [err] = writeSPMmat (matin, [matout]);
00004 %
00005 %Purpose:
00006 %   Write an ascii version of SPM's .mat files
00007 %   containing a 4x4 transformation matrix.
00008 %   The output of this function can be used
00009 %   with 3dWarp to create AFNI-formatted data from
00010 %   SPM datasets.
00011 %
00012 %Input Parameters:
00013 %   matin : Name of .mat file containing transform
00014 %   matout: (optional) Name of output file.
00015 %           If matout is not specified, the _ASCII
00016 %           extension is added to matin
00017 %           
00018 %Output Parameters:
00019 %   err : 0 No Problem
00020 %       : 1 Problems
00021 %   
00022 %   
00023 %More Info :
00024 %   3dWarp -help
00025 %   http://afni.nimh.nih.gov/ssc/ziad/SUMA/SUMA_doc.htm
00026 %   (search for SPM)
00027 %
00028 %     Author : Ziad Saad
00029 %     Date : Thu Aug 7 11:31:54 EDT 2003
00030 %     SSCC/NIMH/ National Institutes of Health, Bethesda Maryland
00031 
00032 
00033 %Define the function name for easy referencing
00034 FuncName = 'writeSPMmat';
00035 
00036 %Debug Flag
00037 DBG = 1;
00038 
00039 %initailize return variables
00040 err = 1;
00041 
00042 %does the input file exist ?
00043 matin = deblank(matin);
00044 matin = deblank(fliplr(matin));
00045 matin = fliplr(matin);
00046 
00047 if (exist(matin) ~= 2),
00048    fprintf(2,'Error %s:\nFile %s not found.\n', FuncName, matin);
00049    return;
00050 end
00051 
00052 %get its prefix
00053 if (strmatch('tam.', fliplr(matin)) == 1),
00054    prefixin = matin(1:length(matin)-4);
00055 else 
00056    prefixin = matin;
00057 end
00058 
00059 %load it 
00060 S = load (matin);
00061 if (~isfield(S,'M')),
00062    fprintf(2,'Error %s:\nFailed to find M in %s\n', FuncName, matin);
00063    return;
00064 end
00065 
00066 if (size(S.M,2) ~= 4),
00067    fprintf(2,'Error %s:\nM does not have 4 columns!\nM''s size is %g x %g',... 
00068       FuncName, size(S.M,1), size(S.M,2));
00069    return;
00070 end
00071 
00072 if (size(S.M,1) ~= 3 & size(S.M,1) ~= 4),
00073    fprintf(2,'Error %s:\nM does not have 3 or 4 rows!\nM''s size is %g x %g',... 
00074       FuncName, size(S.M,1), size(S.M,2));
00075    return;
00076 end
00077 
00078 if (size(S.M,1) == 4),
00079    if (S.M(4,1) ~= 0 | S.M(4,2) ~= 0 | S.M(4,3) ~= 0 | S.M(4,4) ~= 1),
00080       beep;
00081       fprintf(2,'Warning %s:\nAFNI expects the 4th row of M to be [0 0 0 1].\n', FuncName);
00082       fprintf(2,'This row is currently [%g %g %g %g] and will be ignored by AFNI.\n', ...
00083          S.M(4,1), S.M(4,2), S.M(4,3), S.M(4,4)); 
00084       fprintf(2,'Proceed with caution.\n');
00085       beep;
00086    end
00087 end
00088 
00089 %write output file
00090 if (nargin == 1),
00091    matout = [prefixin, '_ASCII.mat'];
00092 end
00093 if (exist(matout) == 2),
00094    fprintf(2,'Error %s:\nOutput file %s exists already.\nWill not overwrite.\n',...
00095     FuncName, matout);
00096    return;
00097 end
00098 
00099 fid = fopen(matout,'w');
00100 if (fid < 0),
00101    fprintf(2,'Error %s:\nFailed in opening %s for writing.\nCheck permissions and disk space.\n',...
00102       FuncName, matout);
00103    return;
00104 end
00105 fprintf (fid,'#Conversion of SPM''s %s \n', matin);
00106 fprintf (fid,'# transformation matrix file to ascii format.\n#\n');
00107 fprintf (fid,'#To convert SPM datasets to AFNI''s format, use \n');
00108 fprintf (fid,'# 3dWarp including these options:\n');
00109 fprintf (fid,'# -matvec_in2out %s -matvec_fsl \n', matout);
00110 fprintf (fid,'#See 3dWarp -help for more info.\n#\n');
00111 for (i=1:1:4),
00112    fprintf (fid,'\t%f\t%f\t%f\t%f\n',... 
00113       S.M(i,1), S.M(i,2), S.M(i,3), S.M(i,4));
00114 end
00115 
00116 fclose(fid);
00117 err = 0;
00118 return;
00119 
 

Powered by Plone

This site conforms to the following standards: