freesurfer_write_surf - FreeSurfer I/O function to write a surface file freesurfer_write_surf(fname, vert, face) writes a surface triangulation into a binary file fname - name of file to write vert - Nx3 matrix of vertex coordinates face - Mx3 matrix of face triangulation indices The face matrix here must be matlab compatible (no zero indices). It is converted to FreeSurfer indices that start at zero. See also freesurfer_read_surf, freesurfer_write_curv, freesurfer_write_wfile
0001 function freesurfer_write_surf(fname, vert, face) 0002 0003 % freesurfer_write_surf - FreeSurfer I/O function to write a surface file 0004 % 0005 % freesurfer_write_surf(fname, vert, face) 0006 % 0007 % writes a surface triangulation into a binary file 0008 % fname - name of file to write 0009 % vert - Nx3 matrix of vertex coordinates 0010 % face - Mx3 matrix of face triangulation indices 0011 % 0012 % The face matrix here must be matlab compatible 0013 % (no zero indices). It is converted to FreeSurfer 0014 % indices that start at zero. 0015 % 0016 % See also freesurfer_read_surf, freesurfer_write_curv, freesurfer_write_wfile 0017 0018 if(nargin ~= 3) 0019 fprintf('USAGE: freesurfer_write_surf(fname, vert, face)\n'); 0020 return; 0021 end 0022 0023 if size(vert,2) ~= 3, 0024 error('vert must be Nx3 matrix'); 0025 end 0026 0027 if size(face,2) ~= 3, 0028 error('face must be Mx3 matrix'); 0029 end 0030 0031 fprintf('...subtracting 1 from face indices for FreeSurfer compatibility.\n'); 0032 face = face - 1; 0033 0034 % open it as a big-endian file 0035 fid = fopen(fname, 'wb', 'b') ; 0036 0037 TRIANGLE_FILE_MAGIC_NUMBER = 16777214 ; 0038 freesurfer_fwrite3(fid, TRIANGLE_FILE_MAGIC_NUMBER); 0039 0040 vnum = size(vert,1) ; % number of vertices 0041 fnum = size(face,1) ; % number of faces 0042 0043 % Ouput a couple of text lines with creation date 0044 str = sprintf('created from matalb on %s\n',datestr(now)); 0045 fwrite(fid, str,'char'); 0046 fwrite(fid, str,'char'); 0047 0048 fwrite(fid, vnum,'int32'); 0049 fwrite(fid, fnum,'int32'); 0050 0051 % reshape vert into column array and write 0052 vert = reshape(vert',size(vert,1)*size(vert,2),1); 0053 fwrite(fid, vert,'float32'); 0054 0055 % reshape face into column array and write 0056 face = reshape(face',size(face,1)*size(face,2),1); 0057 fwrite(fid, face,'int32'); 0058 0059 fclose(fid) ; 0060 0061 return