Doxygen Source Code Documentation
WriteBrikHEAD.m
Go to the documentation of this file.00001 function [err, ErrMessage] = WriteBrikHEAD (FileName, Info)
00002 %
00003 % [err, ErrMessage] = WriteBrikHEAD (FnameHEAD, Info)
00004 %
00005 %Purpose:
00006 % Create the header file for an AFNI Brik
00007 %
00008 %
00009 %Input Parameters:
00010 % FnameHEAD the name of the header file
00011 % Info, a structure detailed in function BrikInfo
00012 %
00013 %Output Parameters:
00014 % err : 0 No Problem, or warning
00015 % : 1 Mucho Problems
00016 % ErrMessage: the warning or error message
00017 %
00018 %
00019 %Key Terms:
00020 %
00021 %More Info :
00022 %
00023 % see HEAD_Rules function
00024 %
00025 % This function is meant to be called by WriteBrik function.
00026 % It will overwrite an existing FnameHEAD so make sure you check
00027 % for overwrite in the calling function.
00028 %
00029 % This function deletes any existing IDCODE_STRING
00030 % It also sets up a new date for the field IDCODE_DATE
00031 %
00032 % Any fiedls in Info will get written to the .HEAD file.
00033 % AFNI does not mind adding new fields, but be neat it won't hurt.
00034 %
00035 % The fields generated by BrikInfo are removed here.
00036 %
00037 % see the files
00038 % BrikInfo, CheckBrikHEAD, WriteBrik
00039 %
00040 % NOTE: This function does not perform any checks to determine
00041 % if the fiedls are appropriate for AFNI. The checks are performed
00042 % in WriteBrik
00043 %
00044 % Because these files are not expected to be large, there is no option to
00045 % replace a certain field. The entire .HEAD is rewritten each time.
00046 %
00047 % version 2.0
00048 %
00049 % Author : Ziad Saad
00050 % Date : Thu Sep 14 16:25:27 PDT 2000
00051 % LBC/NIMH/ National Institutes of Health, Bethesda Maryland
00052
00053 FUNCTION_VERSION = 'V2.0';
00054
00055 %Define the function name for easy referencing
00056 FuncName = 'WriteBrikHEAD';
00057
00058 %Debug Flag
00059 DBG = 1;
00060 Csep = filesep;
00061
00062 %initailize return variables
00063 err = 1;
00064 ErrMessage = '';
00065
00066 %cleanup fields generated by BrikInfo that are irrelevant to AFNI
00067 if (isfield(Info, 'RootName')), Info = rmfield(Info,'RootName') ; end
00068 if (isfield(Info, 'TypeName')), Info = rmfield(Info,'TypeName') ; end
00069 if (isfield(Info, 'TypeBytes')), Info = rmfield(Info,'TypeBytes') ; end
00070 if (isfield(Info, 'ByteOrder')), Info = rmfield(Info,'ByteOrder') ; end
00071 if (isfield(Info, 'Orientation')), Info = rmfield(Info,'Orientation') ; end
00072 %if (isfield(Info, '')), Info = rmfield(Info,'') ; end
00073
00074
00075 %reset the IDcode, eventually, you'll create one here
00076 if (isfield(Info, 'IDCODE_STRING')),
00077 Info = rmfield(Info, 'IDCODE_STRING');
00078 end
00079
00080 %setup a new date
00081 Info.IDCODE_DATE = datestr(now,0);
00082
00083 %add the generating code version
00084 Info.GEN_SOURCE = sprintf('WriteBrik matlab library functions %s (please report bugs to ziad@nih.gov)', FUNCTION_VERSION);
00085
00086
00087 fidout = fopen(FileName, 'w');
00088 if (fidout < 0),
00089 err = 1; ErrMessage = sprintf('Error %s: Could not open %s for writing \n', FuncName, FileName); errordlg(ErrMessage); return;
00090 end
00091
00092 Fld_Allnames = fieldnames(Info);
00093 Fld_num = size(Fld_Allnames,1);
00094
00095 for (i=1:1:Fld_num),
00096 Fld_Name = char(Fld_Allnames(i));
00097 Fld_Val = getfield(Info, Fld_Name);
00098 Fld_isstrn = 0;
00099 [err, ErrMessage, Rules] = HEAD_Rules(Fld_Name);
00100 RulesType = Rules.isNum;
00101 switch (RulesType),
00102 case -1, %unknown type, guess
00103 if (ischar(Fld_Val)),
00104 %tis a string
00105 Fld_isstrn = 1;
00106 Fld_Val = zdeblank(Fld_Val);
00107 Fld_Type = 'string-attribute';
00108 Fld_Count = length(Fld_Val)+1;
00109 elseif (isstruct(Fld_Val)),
00110 %tis a structure
00111 err = 1;
00112 ErrMessage = sprintf('%s: Cannot handle structures', FuncName);
00113 return;
00114 elseif (isint(Fld_Val)),
00115 %tis an integer
00116 Fld_Type = 'integer-attribute';
00117 Fld_Count = length(Fld_Val(:));
00118 else
00119 %tis a float
00120 Fld_Type = 'float-attribute';
00121 Fld_Count = length(Fld_Val(:));
00122 end
00123 case 0, %char
00124 %tis a string
00125 Fld_isstrn = 1;
00126 Fld_Val = zdeblank(Fld_Val);
00127 Fld_Type = 'string-attribute';
00128 Fld_Count = length(Fld_Val)+1;
00129 case 1, %int
00130 %tis an integer
00131 Fld_Type = 'integer-attribute';
00132 Fld_Count = length(Fld_Val(:));
00133 case 2, %float
00134 %tis a float
00135 Fld_Type = 'float-attribute';
00136 Fld_Count = length(Fld_Val(:));
00137 otherwise,
00138 err = 1; ErrMessage = sprintf('Error %s: RulesType %d unknown.', FuncName, RulesType); errordlg(ErrMessage); return;
00139 end
00140 %write them out
00141 fprintf(fidout,'\ntype = %s\n', Fld_Type);
00142 fprintf(fidout,'name = %s\n', Fld_Name);
00143 fprintf(fidout,'count = %g\n', Fld_Count);
00144 if(Fld_isstrn),
00145 %string to write out, do what Bob seems to do
00146 stmp = sprintf('''%s~',Fld_Val);
00147 fprintf(fidout,'%s\n', stmp);
00148 else
00149 %write out five values per line, if field is a matrix, reshape it to a
00150 %Nx1 vector
00151 nsz = size(Fld_Val); npts = nsz(1).*nsz(2);
00152 Fld_Val = reshape(Fld_Val, npts, 1);
00153 %print out 5 values per line
00154 fprintf(fidout, '\t%g\t%g\t%g\t%g\t%g\n', Fld_Val);
00155 if (rem(npts,5)), fprintf(fidout,'\n'); end
00156 end
00157 end
00158
00159 fclose (fidout);
00160
00161 err = 0;
00162
00163 return;
00164