@@ -139,16 +139,42 @@ function bids_writeelectrodefile(EEG, fileOut, varargin)
139139 values{end + 1 } = ' n/a' ;
140140 values{end + 1 } = ' n/a' ;
141141 else
142- values{end + 1 } = sprintf(' %2.6f ' , EEG .chanlocs(iChan ).X);
143- values{end + 1 } = sprintf(' %2.6f ' , EEG .chanlocs(iChan ).Y);
142+ % Determine decimal precision based on coordinate units
143+ if isEMG && isfield(EEG .chaninfo , ' BIDS' ) && isfield(EEG .chaninfo .BIDS , ' EMGCoordinateUnits' )
144+ units = EEG .chaninfo .BIDS .EMGCoordinateUnits ;
145+ if strcmpi(units , ' percent' )
146+ fmt = ' %.0f ' ; % No decimals for percent
147+ elseif strcmpi(units , ' ratio' )
148+ fmt = ' %.2f ' ; % Max 2 decimals for ratio
149+ else
150+ fmt = ' %2.6f ' ; % Default: 6 decimals for mm, etc.
151+ end
152+ else
153+ fmt = ' %2.6f ' ; % Default
154+ end
155+ values{end + 1 } = sprintf(fmt , EEG .chanlocs(iChan ).X);
156+ values{end + 1 } = sprintf(fmt , EEG .chanlocs(iChan ).Y);
144157 end
145158
146159 % Z (only if column exists)
147160 if ismember(' z' , columnsToWrite )
148161 if isempty(EEG .chanlocs(iChan ).X) || isnan(EEG .chanlocs(iChan ).X) || contains(fileOut , ' ieeg' )
149162 values{end + 1 } = ' n/a' ;
150163 else
151- values{end + 1 } = sprintf(' %2.6f ' , EEG .chanlocs(iChan ).Z);
164+ % Use same format as X/Y
165+ if isEMG && isfield(EEG .chaninfo , ' BIDS' ) && isfield(EEG .chaninfo .BIDS , ' EMGCoordinateUnits' )
166+ units = EEG .chaninfo .BIDS .EMGCoordinateUnits ;
167+ if strcmpi(units , ' percent' )
168+ fmt = ' %.0f ' ;
169+ elseif strcmpi(units , ' ratio' )
170+ fmt = ' %.2f ' ;
171+ else
172+ fmt = ' %2.6f ' ;
173+ end
174+ else
175+ fmt = ' %2.6f ' ;
176+ end
177+ values{end + 1 } = sprintf(fmt , EEG .chanlocs(iChan ).Z);
152178 end
153179 end
154180
@@ -280,7 +306,14 @@ function bids_writeelectrodefile(EEG, fileOut, varargin)
280306 coordsystemStruct.EEGCoordinateSystemDescription = ' EEGLAB' ;
281307 end
282308 end
283- jsonwrite( [ fileOut ' _coordsystem.json' ], coordsystemStruct , struct(' indent' ,' ' ));
309+ % Write coordsystem file
310+ % For EMG with single coordinate system, write at root if rootdir provided
311+ if isEMG && ~isempty(opt .rootdir )
312+ filename = fullfile(opt .rootdir , ' coordsystem.json' );
313+ else
314+ filename = [fileOut ' _coordsystem.json' ];
315+ end
316+ jsonwrite(filename , coordsystemStruct , struct(' indent' ,' ' ));
284317 end
285318end
286319
0 commit comments