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

% FIELDTRIP2CTF saves a FieldTrip data structures to a corresponding CTF file. The
% file to which the data is exported depends on the input data structure that you
% provide.
%
% Use as
%   fieldtrip2ctf(filename, data, ...)
% where "filename" is a string, "data" is a FieldTrip data structure, and

    
function fieldtrip2ctf(filename, data, varargin)

% FIELDTRIP2CTF saves a FieldTrip data structures to a corresponding CTF file. The
% file to which the data is exported depends on the input data structure that you
% provide.
%
% Use as
%   fieldtrip2ctf(filename, data, ...)
% where "filename" is a string, "data" is a FieldTrip data structure, and
% additional options can be specified as key-value pairs.
%
% The FieldTrip "montage" structure (see FT_APPLY_MONTAGE and the cfg.montage
% option in FT_PREPROCESSING) can be exported to a CTF "Virtual Channels" file.
%
% At this moment the support for other FieldTrip structures and CTF fileformats is
% still limited, but this function serves as a placeholder for future improvements.
%
% See also FT_VOLUMEWRITE, FT_SOURCEWRITE, FT_WRITE_DATA

% Copyright (C) 2015, Robert Oostenveld
%
% This file is part of FieldTrip, see http://www.fieldtriptoolbox.org
% for the documentation and details.
%
%    FieldTrip is free software: you can redistribute it and/or modify
%    it under the terms of the GNU General Public License as published by
%    the Free Software Foundation, either version 3 of the License, or
%    (at your option) any later version.
%
%    FieldTrip is distributed in the hope that it will be useful,
%    but WITHOUT ANY WARRANTY; without even the implied warranty of
%    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
%    GNU General Public License for more details.
%
%    You should have received a copy of the GNU General Public License
%    along with FieldTrip. If not, see <http://www.gnu.org/licenses/>.
%
% $Id$

type = ft_datatype(data);
switch type
  case 'montage'
    % Virtual channels are weighted linear combinations of real channels collected
    % by the CTF MEG System
    
    % rename it for convenience
    montage = data; clear data;
    
    fid = fopen(filename, 'wt');
    assert(fid>0, 'could not open file for writing');
    fprintf(fid, '// Virtual channel configuration\n');
    for i=1:length(montage.labelnew)
      sel = find(montage.tra(i,:) ~= 0);
      fprintf(fid, '\n');
      fprintf(fid, 'VirtualChannel\n');
      fprintf(fid, '{\n');
      fprintf(fid, 'Name:\t%s\n', montage.labelnew{i});
      fprintf(fid, 'Unit:\n');
      for j=1:numel(sel)
        fprintf(fid, 'Ref:\t%s,%f\n', montage.labelold{sel(j)}, montage.tra(i,sel(j)));
      end