mesh_freesurfer2matlab - Read FreeSurfer tesselation (.txt) USEAGE: [FV] = mesh_freesurfer2matlab(file) This function will load an ascii file that contains a one line specification of the number of vertices and faces, followed by rows of vertex points and then rows of face indices into the vertex rows. Each vertex row contains 3 x,y,z coordinates and a colour/potential value. Each face row contains three vertex indices followed by a colour/potential value. Vertices in the .txt file are indexed from zero, but those returned are indexed from one. See also the mesh_freesurferTRI2matlab function to load the data from the BEM .tri tesselations, which have a different text format from those of the .txt/.asc files that are created by the mris_convert function of freesurfer. The freesurfer tesselations may contain too many faces for efficient computations. If so, try 'reducepatch'. The returned FV struct contains fields FV.vertices and FV.faces, which can be input to the patch command, like so: Hpatch = patch('Vertices',FV.vertices,'Faces',FV.faces,... 'EdgeColor',[.8 .8 .8],'FaceColor',[0.9 0.9 0.9]); This will plot the mesh as a patch object. See the patch command and matlab help for more information on coloring this object. Freesurfer website: http://surfer.nmr.mgh.harvard.edu/
0001 function [FV] = mesh_freesurfer2matlab(file) 0002 0003 % mesh_freesurfer2matlab - Read FreeSurfer tesselation (.txt) 0004 % 0005 % USEAGE: [FV] = mesh_freesurfer2matlab(file) 0006 % 0007 % This function will load an ascii file that contains a one 0008 % line specification of the number of vertices and faces, 0009 % followed by rows of vertex points and then rows of face 0010 % indices into the vertex rows. Each vertex row contains 0011 % 3 x,y,z coordinates and a colour/potential value. Each 0012 % face row contains three vertex indices followed by a 0013 % colour/potential value. Vertices in the .txt file are 0014 % indexed from zero, but those returned are indexed from 0015 % one. 0016 % 0017 % See also the mesh_freesurferTRI2matlab function to load 0018 % the data from the BEM .tri tesselations, which have a 0019 % different text format from those of the .txt/.asc files 0020 % that are created by the mris_convert function of freesurfer. 0021 % 0022 % The freesurfer tesselations may contain too many faces 0023 % for efficient computations. If so, try 'reducepatch'. 0024 % 0025 % The returned FV struct contains fields FV.vertices and 0026 % FV.faces, which can be input to the patch command, like so: 0027 % 0028 % Hpatch = patch('Vertices',FV.vertices,'Faces',FV.faces,... 0029 % 'EdgeColor',[.8 .8 .8],'FaceColor',[0.9 0.9 0.9]); 0030 % 0031 % This will plot the mesh as a patch object. See the patch command 0032 % and matlab help for more information on coloring this object. 0033 % 0034 % Freesurfer website: http://surfer.nmr.mgh.harvard.edu/ 0035 % 0036 0037 % $Revision: 1.1 $ $Date: 2004/11/12 01:32:35 $ 0038 0039 % Licence: GNU GPL, no implied or express warranties 0040 % History: 03/02 Darren.Weber_at_radiology.ucsf.edu 0041 % 0042 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 0043 0044 fid = fopen(file,'r'); 0045 0046 if isequal(fid,-1), 0047 S=sprintf('Could not open file: "%s"',file); 0048 error(S); 0049 else 0050 0051 fprintf('...Reading FreeSurfer Tesselation Data\n'); 0052 0053 tic; 0054 0055 % Check for comment on first line of file 0056 frewind(fid); temp = fscanf(fid,'%s',1); frewind(fid); 0057 if findstr(temp,'#'), temp = fgetl(fid); end 0058 0059 % Read number of vertices/faces 0060 Nvertices = fscanf(fid,'%d',1); 0061 Nfaces = fscanf(fid,'%d',1); 0062 0063 % Read vertices 0064 fprintf('...Reading %d Vertices\n',Nvertices); 0065 vertices = fscanf(fid,'%f',[4,Nvertices]); 0066 % remove last row (all zeros) and translate 0067 FV.vertices = vertices(1:3,:)'; 0068 0069 % Read faces 0070 fprintf('...Reading %d Faces\n',Nfaces); 0071 faces = fscanf(fid,'%d',[4,Nfaces]); 0072 % remove last row (all zeros), translate and 0073 % add 1 because FreeSurfer vertices start at zero 0074 FV.faces = faces(1:3,:)' + 1; 0075 0076 fclose(fid); 0077 0078 t = toc; 0079 fprintf('...done (%6.2f sec).\n',t); 0080 0081 end 0082 0083 return