elec_write_emse - Write an EMSE probe file (*.elp) Usage: [p] = elec_write_emse(p) This script outputs x,y,z values to an EMSE probe (*.elp) file. EMSE *.elp files are in meters. Also, when using EMSE *.elp files in the eeg_toolbox, it is necessary to swap X and Y. See also: emse_read_elp, elec_open
0001 function [p] = elec_write_emse(p) 0002 0003 % elec_write_emse - Write an EMSE probe file (*.elp) 0004 % 0005 % Usage: [p] = elec_write_emse(p) 0006 % 0007 % This script outputs x,y,z values to an EMSE 0008 % probe (*.elp) file. 0009 % 0010 % EMSE *.elp files are in meters. Also, when using 0011 % EMSE *.elp files in the eeg_toolbox, it is necessary 0012 % to swap X and Y. 0013 % 0014 % See also: emse_read_elp, elec_open 0015 % 0016 0017 % $Revision: 1.2 $ $Date: 2005/07/12 21:28:23 $ 0018 0019 % Licence: GNU GPL, no express or implied warranties 0020 % History: 10/2002, Darren.Weber_at_radiology.ucsf.edu 0021 % 0022 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 0023 0024 [path,name,ext] = fileparts(strcat(p.elec.path,filesep,p.elec.file)); 0025 ext = '.elp'; 0026 file = fullfile(path,[name ext]); 0027 0028 p.elec.file = [name ext]; 0029 0030 [fid,msg] = fopen(file,'w','ieee-le'); 0031 if ~isempty(msg), error(msg); end 0032 0033 tic 0034 0035 fprintf('\nELEC_WRITE_EMSE...\n'); 0036 fprintf('...writing .elp data to:\n\t%s\n',file); 0037 0038 write_elp(fid,p); 0039 0040 t = toc; fprintf('...done (%6.2f sec).\n\n',t); 0041 0042 return 0043 0044 0045 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 0046 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 0047 function [elp] = write_elp(fid,p) 0048 0049 Nelec = size(p.elec.data.x,1); 0050 0051 % Probe files contain position information for electrode locations 0052 % and/or gradiometer locations. The file consists of a prolog, a 0053 % header, and a list of one or more sensor fields. 0054 0055 % Any line beginning with '//' is a comment line, which is ignored 0056 0057 % Write the prolog 0058 fprintf(fid,'%d %d\n%d\n',3,2,1); 0059 0060 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 0061 % Write the header 0062 % The header consists of one optional entry and 2 entries in 0063 % mandatory sequence and one optional entry: 0064 % Name [optional] > %N %s replace %s with name string (8 or fewer characters) 0065 % Type Code > %x replace %x with 1 (all electric), 2 (all magnetic) or 4 (mixed). 0066 % #Channels > %d number of points per channel per epoch [???? DLW] 0067 0068 fprintf(fid,'//TypeCode nsensors\n'); 0069 fprintf(fid,'%d %d\n',1,Nelec + 1); % Nelectrodes plus REF 0070 0071 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 0072 % Fiducial points may be included optionally. They are required 0073 % for MRI registration. If they are included, they must be in 0074 % the obligatory order : nasion, left preauricular point, 0075 % right preauricular point. Table A-2 defines the format for 0076 % representing fiduciary points. 0077 0078 fprintf(fid,'//Fiducials (Nasion, Left Preauricular, Right Preauricular)\n'); 0079 fprintf(fid,'%s\t%12.8f\t%12.8f\t%12.8f\n','%F',... 0080 p.elec.data.nasion(1),... 0081 p.elec.data.nasion(2),... 0082 p.elec.data.nasion(3)); 0083 fprintf(fid,'%s\t%12.8f\t%12.8f\t%12.8f\n','%F',... 0084 p.elec.data.lpa(1),... 0085 p.elec.data.lpa(2),... 0086 p.elec.data.lpa(3)); 0087 fprintf(fid,'%s\t%12.8f\t%12.8f\t%12.8f\n','%F',... 0088 p.elec.data.rpa(1),... 0089 p.elec.data.rpa(2),... 0090 p.elec.data.rpa(3)); 0091 0092 % Each electrode is represented by an electric sensor, 0093 % and consists of 5 fields, of which 1 (the name) is 0094 % optional. 0095 % Name Format Description 0096 % Type Code %S %x replace %x with 400 (electrode) or 1c00 if reference channel 0097 % Name [optional] %N %s replace %s with name string (8 or fewer characters) 0098 % Position %g %g %g electrode location with respect to head frame (Cartesian, meters) 0099 % Orientation %g %g %g not used, replace with 0 0 1 0100 % 0101 % Sensor state (which appears in the 'type code' field) may 0102 % be obtained by logically OR-ing suitable combinations from 0103 % the table below. Note that not all combinations are physically valid. 0104 % 0105 % type/state type code 0106 % magnetic 200 0107 % electric 400 0108 % off 800 0109 % reference 1000 actually '1c00' [DLW] 0110 % optical 4000 0111 % trigger 8000 0112 % other 10000 0113 % named point 20000 0114 % 0115 % Other types (such as named points, trigger, and optical) should 0116 % be represented in the same pattern as electrodes, with the type 0117 % code set to identify the type. Even those types (e.g. trigger) 0118 % which do not have a true location, should have a nominal 0119 % location, (e.g. 0 0 0). 0120 0121 0122 % Note below that eeg_toolbox x,y,z are equivalent to 0123 % emse y,-x,z 0124 0125 0126 typecode = 400; 0127 0128 for n = 1:Nelec, 0129 fprintf(fid,'\n//ecSensor typecode/state---------------------\n'); 0130 fprintf(fid,'%s\t%d\n','%S',typecode); 0131 fprintf(fid,'//ecSensor name:\n'); 0132 fprintf(fid,'%s\t%s\n','%N',char(p.elec.data.label(n))); 0133 fprintf(fid,'//sphere origin\n'); 0134 fprintf(fid,'%s\t%12.8f\t%12.8f\t%12.8f\n','%O',... 0135 p.elec.data.centroid(2),... 0136 p.elec.data.centroid(1) * -1,... 0137 p.elec.data.centroid(3)); 0138 fprintf(fid,'//ecSensor location (origin)\n'); 0139 fprintf(fid,'%s\t%12.8f\t%12.8f\t%12.8f\n',' ',... 0140 p.elec.data.y(n),... 0141 p.elec.data.x(n) * -1,... 0142 p.elec.data.z(n)); 0143 end 0144 0145 % Output the REF 0146 typecode = '1c00'; % This is not consistent with the 0147 % table above, but it works! 0148 fprintf(fid,'\n//ecSensor typecode/state---------------------\n'); 0149 fprintf(fid,'%s\t%s\n','%S',typecode); 0150 fprintf(fid,'//ecSensor name:\n'); 0151 fprintf(fid,'%s\t%s\n','%N','Ref'); 0152 fprintf(fid,'//sphere origin\n'); 0153 fprintf(fid,'%s\t%12.8f\t%12.8f\t%12.8f\n','%O',... 0154 p.elec.data.centroid(2),... 0155 p.elec.data.centroid(1) * -1,... 0156 p.elec.data.centroid(3)); 0157 fprintf(fid,'//ecSensor location (origin)\n'); 0158 fprintf(fid,'%s\t%12.8f\t%12.8f\t%12.8f\n',' ',... 0159 p.elec.data.ref(2),... 0160 p.elec.data.ref(1) * -1,... 0161 p.elec.data.ref(3)); 0162 0163 fclose(fid); 0164 0165 return