ELEC_LOAD_SCANTRI - read sensor coordinates from Scan .tri files INPUTS: file - string, name of the .tri file OUTPUTS: tri.hdr - header and electrode fields tri.XYZ - electrode coordinates (tri.hdr.elec) tri.label - first 4 letters of electrode names Also check small script at the end of the code to print out coordinates together with names (a fast hack) Information on Neuroscan .tri format is at http://www.neuro.com/neuroscan/triformat.htm, which is copied at the end of this function .m file .tri faces and vertices are not returned in this version
0001 function [tri] = elec_load_scan_tri(file) 0002 0003 % ELEC_LOAD_SCANTRI - read sensor coordinates from Scan .tri files 0004 % 0005 % INPUTS: 0006 % file - string, name of the .tri file 0007 % OUTPUTS: 0008 % tri.hdr - header and electrode fields 0009 % tri.XYZ - electrode coordinates (tri.hdr.elec) 0010 % tri.label - first 4 letters of electrode names 0011 % 0012 % Also check small script at the end of the code to print 0013 % out coordinates together with names (a fast hack) 0014 % 0015 % Information on Neuroscan .tri format is at 0016 % http://www.neuro.com/neuroscan/triformat.htm, which 0017 % is copied at the end of this function .m file 0018 % 0019 % .tri faces and vertices are not returned in this version 0020 % 0021 0022 % $Revision: 1.1 $ $Date: 2004/11/12 01:32:33 $ 0023 0024 % Licence: GNU GPL, no express or implied warranties 0025 % History: 10/2002, Yaroslav Halchenko, CS Dept. UNM 0026 % (yoh@onerussian.com, ICQ#: 60653192) 0027 % 01/2003, Darren.Weber_at_radiology.ucsf.edu 0028 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 0029 0030 fid = fopen(file,'r'); 0031 0032 if isequal(fid,-1), 0033 msg = sprintf('Could not open file: "%s"',file); 0034 error(msg); 0035 else 0036 0037 fprintf('...Reading NeuroScan Tesselation (.tri)'); 0038 0039 tri.hdr.ID = fread(fid,1,'long'); % Long ID (should be 100003, or 100004. (or 100002) ) 0040 tri.hdr.filetype = fread(fid,1,'short'); % Short Filetype (=2 for triangle file) 0041 tri.hdr.rev = fread(fid,1,'short'); % short revision 0042 tri.hdr.elthick = fread(fid,1,'float'); % float electrodethickness 0043 tri.hdr.eldiam = fread(fid,1,'float'); % float electrodediameter 0044 tri.hdr.reserved = fread(fid, 4080,'char'); % BYTE reserved[4080] 0045 0046 % Then the face and vertex information follows: 0047 0048 tri.hdr.Nfaces = fread(fid,1,'short'); 0049 tri.hdr.Nvertices = fread(fid,1,'short'); 0050 0051 % Then for all the faces, 0052 % the centroid (centre of face) x,y,z coordinates (unit vector) 0053 % and it's length are writen as four floats 0054 centroid = fread(fid,4* tri.hdr.Nfaces,'float'); 0055 % tri.centroid = reshape(centroid,tri.hdr.Nfaces,4); 0056 0057 % Then follows the vertex coordinates, 0058 % x, y, z, (normalized) and it's length 0059 vertices = fread(fid,4* tri.hdr.Nvertices,'float'); 0060 0061 % vertices = reshape(vertices,tri.hdr.Nvertices,4); 0062 % tri.vert = zeros(tri.hdr.Nvertices,3); 0063 % tri.vert(:,1) = vertices(:,1) .* vertices(:,4); 0064 % tri.vert(:,2) = vertices(:,2) .* vertices(:,4); 0065 % tri.vert(:,3) = vertices(:,3) .* vertices(:,4); 0066 0067 % Then for all the faces, the three vertices that belong to them 0068 faces = fread(fid,3* tri.hdr.Nfaces,'short'); 0069 % tri.faces = reshape(faces,tri.hdr.Nfaces,3) + 1; % add 1 for matlab 0070 0071 %Then the number of electrodes follows 0072 tri.hdr.Nelectrodes = fread(fid,1,'ushort'); 0073 0074 % Then for all electrodes 0075 for e = 1:tri.hdr.Nelectrodes, 0076 tri.hdr.elec(e).label = fread(fid,10,'char')'; % label of electrode, max 9 chars + \0 0077 tri.hdr.elec(e).key = fread(fid,1,'short'); % key, normally = 'e' for electrode 0078 tri.hdr.elec(e).pos = fread(fid,3,'float')'; % x, y, z (position) 0079 tri.hdr.elec(e).index = fread(fid,1,'ushort'); % electrode index number 0080 end 0081 0082 fclose(fid); 0083 0084 tmp = char(tri.hdr.elec(:).label); 0085 tri.label = tmp(:,1:4); 0086 0087 tri.XYZ = reshape([tri.hdr.elec(:).pos],3,tri.hdr.Nelectrodes)'; 0088 0089 % tri.XYZ = zeros(e,3); 0090 % for e = 1:tri.hdr.Nelectrodes 0091 % tri.XYZ(e,1:3) = tri.hdr.elec(e).pos; 0092 % end 0093 0094 end 0095 0096 return; 0097 0098 0099 %---------------------------------------------------------------- 0100 0101 fout = fopen('XYZ.locs','w'); 0102 for e = 1:tri.hdr.Nelectrodes, 0103 nop = 0; 0104 for z=1:10, 0105 if (tri.hdr.elec(e).label(z)==0), nop=1; end 0106 if (~nop), 0107 fprintf(fout,'%c',tri.hdr.elec(e).label(z)); 0108 else 0109 fprintf(fout,' '); 0110 end 0111 end 0112 0113 fprintf(fout, '%8.3f ', tri.hdr.elec(e).pos); 0114 fprintf(fout, '\n'); 0115 end 0116 fclose(fout); 0117 0118 return 0119 0120 % -------------------------------------------------------------------------------- 0121 0122 0123 % 3D Space TRI file format 0124 % 0125 % Below you will find the description of the TRI 0126 % file format as used in the 3D Space program. The 0127 % TRI file contains the description of a triangulated 0128 % head surface. The description uses C/C++ variables 0129 % to describe elements. 0130 % 0131 % 0132 % -------------------------------------------------------- 0133 % 0134 % Each file starts with a header, which looks like this: 0135 % 0136 % Long ID (should be 100003, or 100004. (or 100002) ) 0137 % Short Filetype (=2 for triangle file) 0138 % short revision 0139 % float electrodethickness 0140 % float electrodediameter 0141 % BYTE reserved[4080] 0142 % 0143 % Then the facet and vertex information follows: 0144 % 0145 % short number_of_facets 0146 % short number_of_vertices 0147 % 0148 % Then for all the facets give by number_of_facets: 0149 % 0150 % the centroid (centre of facet) x,y,z coordinates 0151 % (unit vector) and it's length are writen as four floats. 0152 % 0153 % Then follows the facet vertex coordinates for all 0154 % vertices (given by number_of_vertices): 0155 % 0156 % four floats: x, y, z, (normalized) and it's length 0157 % 0158 % Then for all the facets (give by number_of_facets): 0159 % 0160 % the three vertices that belong to this facet. These 0161 % are three shorts, and are index values to the proper 0162 % vertice as listed above. (largest number is given by 0163 % number_of_vertices). 0164 % 0165 % Then the number of electrodes follows: 0166 % 0167 % (unsigned short) number_of_electrodes 0168 % 0169 % Then for all electrodes (given by number_of_electrodes): 0170 % 0171 % char label[10] (label of electrode, max 9 chars + \0) 0172 % short key (normally = 'e' for electrode) 0173 % float x, y, z (position) 0174 % unsigned short ix (electrode index number) 0175 %