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: spm_eeg_grandmean.m
    path of file: ./repos/spm
    
    the code of the file until where you have to start completion: function Do = spm_eeg_grandmean(S)
% Average over multiple M/EEG data sets
% FORMAT Do = spm_eeg_grandmean(S)
%
% S         - struct (optional)
%  fields of S:
%   D         - filenames (char matrix) of M/EEG MAT-files containing
%               epoched data
%   weighted  - average weighted by number of replications in inputs (1)
%               or not (0) [default: 0]
%   outfile   - name of the output file [default: 'grand_mean']
%
% Output:
% Do        - EEG data struct, result files are saved in the same
%             directory as first input file.
%__________________________________________________________________________
%
% spm_eeg_grandmean averages data over multiple files. The data must have
% the same trialtype numbering and sampling rate. This function can be used
% for grand mean averaging, i.e. computing the average over multiple

    
function Do = spm_eeg_grandmean(S)
% Average over multiple M/EEG data sets
% FORMAT Do = spm_eeg_grandmean(S)
%
% S         - struct (optional)
%  fields of S:
%   D         - filenames (char matrix) of M/EEG MAT-files containing
%               epoched data
%   weighted  - average weighted by number of replications in inputs (1)
%               or not (0) [default: 0]
%   outfile   - name of the output file [default: 'grand_mean']
%
% Output:
% Do        - EEG data struct, result files are saved in the same
%             directory as first input file.
%__________________________________________________________________________
%
% spm_eeg_grandmean averages data over multiple files. The data must have
% the same trialtype numbering and sampling rate. This function can be used
% for grand mean averaging, i.e. computing the average over multiple
% subjects. Missing event types and bad channels are taken into account
% properly. The output is written to a user-specified new file. The default
% name is the same name as the first selected input file, but prefixed with
% a 'g'. The output file is written to the current working directory.
%__________________________________________________________________________

% Stefan Kiebel
% Copyright (C) 2008-2022 Wellcome Centre for Human Neuroimaging


%-Startup
%--------------------------------------------------------------------------
spm('FnBanner', mfilename);
spm('FigName','M/EEG Grand Mean'); spm('Pointer','Watch');

if ~isfield(S, 'weighted'),     S.weighted = 0;            end