You are an expert in writing code in many different languages. Your goal is to perform code completion for the following code, keeping in mind the rest of the code and the file meta data.
    Metadata details:
    filename: write_bioimage_mgrid.m
    path of file: ./repos/fieldtrip/fileio/private
    
    the code of the file until where you have to start completion: function write_bioimage_mgrid(filename, elec)

% --------------------------------------------------------
% WRITE_BIOIMAGE_MGRID writes BioImage Suite .mgrid files from a FieldTrip
% elec datatype structure
%
% Use as:
%   write_bioimage_mgrid(filename, elec)
%   where filename has an .mgrid file extension and elec has both a label
%   and an elecpos field
%
% To view the mgrid file in BioImage Suite, ensure that the orientation of
% the scan (e.g., RAS) corresponds with the orientation of the electrode
% positions (in head coordinates) of elec

    
function write_bioimage_mgrid(filename, elec)

% --------------------------------------------------------
% WRITE_BIOIMAGE_MGRID writes BioImage Suite .mgrid files from a FieldTrip
% elec datatype structure
%
% Use as:
%   write_bioimage_mgrid(filename, elec)
%   where filename has an .mgrid file extension and elec has both a label
%   and an elecpos field
%
% To view the mgrid file in BioImage Suite, ensure that the orientation of
% the scan (e.g., RAS) corresponds with the orientation of the electrode
% positions (in head coordinates) of elec
%
% Copyright (C) 2017, Arjen Stolk & Sandon Griffin
% --------------------------------------------------------


% extract info from label field
for e = 1:numel(elec.label) % electrode loop
  ElecStrs{e,1} = regexprep(elec.label{e}, '\d+(?:_(?=\d))?', ''); % without electrode numbers
  ElecNrs(e) = str2double(regexp(elec.label{e},'-?\d+\.?\d*|-?\d*\.?\d+', 'match')); % without electrode strings
  tmp = strfind(elec.label{e}, num2str(ElecNrs(e))); % the position of the first number within the label
  ElecNumPos(e) = tmp:tmp+numel(ElecNrs(e))-1; % the positions of the numbers within the label
end