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: fieldtrip2besa.m
    path of file: ./repos/spm12/external/fieldtrip
    
    the code of the file until where you have to start completion: function fieldtrip2besa(filename, data, varargin)

% FIELDTRIP2BESA saves a FieldTrip data structures to a corresponding BESA file. This
% export function is based on documentation that was provided by Todor Jordanov of
% BESA.
%
% Use as
%   fieldtrip2besa(filename, data)
% with data as obtained from FT_PREPROCESSING to export single trial data as a
% set of .avr files.
%
% Use as
%   fieldtrip2besa(filename, elec)
% or
%   fieldtrip2besa(filename, grad)
% with an electrode structure as obtained from FT_READ_SENS to export channel
% positions to an .elp file.
%
% Additional key-value pairs can be specified according to
%   channel = cell-array, can be used to make subset and to reorder the channels
%
% See also FIELDTRIP2SPSS, FIELDTRIP2FIFF

% parse the optional input arguments
channel = ft_getopt(varargin, 'channel');

% this requires the "MATLAB to BESA Export functions"
% which are available from http://www.besa.de/downloads/matlab/
ft_hastoolbox('matlab2besa', 1);

datatype = ft_datatype(data);
switch datatype


    
function fieldtrip2besa(filename, data, varargin)

% FIELDTRIP2BESA saves a FieldTrip data structures to a corresponding BESA file. This
% export function is based on documentation that was provided by Todor Jordanov of
% BESA.
%
% Use as
%   fieldtrip2besa(filename, data)
% with data as obtained from FT_PREPROCESSING to export single trial data as a
% set of .avr files.
%
% Use as
%   fieldtrip2besa(filename, elec)
% or
%   fieldtrip2besa(filename, grad)
% with an electrode structure as obtained from FT_READ_SENS to export channel
% positions to an .elp file.
%
% Additional key-value pairs can be specified according to
%   channel = cell-array, can be used to make subset and to reorder the channels
%
% See also FIELDTRIP2SPSS, FIELDTRIP2FIFF

% parse the optional input arguments
channel = ft_getopt(varargin, 'channel');

% this requires the "MATLAB to BESA Export functions"
% which are available from http://www.besa.de/downloads/matlab/
ft_hastoolbox('matlab2besa', 1);

datatype = ft_datatype(data);
switch datatype

  case 'raw'
    %% write raw data as *.avr
    assert(isempty(channel), 'channel selection and reordering is not yet supported');

    NumTrials = length(data.trial);
    channel_labels = data.label;
    data_scale_factor = 1.0;
    time_scale_factor = 1.0;
    for iTr = 1:NumTrials
      % Multiply by 1000 to get the time in milliseconds.
      time_samples = data.time{iTr}.*1000;
      % The file name where data should be written.
      file_name = sprintf('%s%03d.avr', filename, iTr);
      % Multiply by 1e15 to get the data in femtoTesla.
      data_matrix = data.trial{iTr}.*1e15; %FIXME
      % Save the data
      besa_save2Avr(custom_path, file_name, data_matrix, time_samples, channel_labels, data_scale_factor, time_scale_factor);
    end