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_artefact.m
    path of file: ./repos/spm12
    
    the code of the file until where you have to start completion: function D = spm_eeg_artefact(S)
% Simple artefact detection, optionally with robust averaging
% FORMAT D = spm_eeg_artefact(S)
%
% S                 - input structure
%
% fields of S:
%   S.mode            'reject' [default]: reject bad channels and trials
%                     'mark': scan the data and create events marking the
%                             artefacts

    
function D = spm_eeg_artefact(S)
% Simple artefact detection, optionally with robust averaging
% FORMAT D = spm_eeg_artefact(S)
%
% S                 - input structure
%
% fields of S:
%   S.mode            'reject' [default]: reject bad channels and trials
%                     'mark': scan the data and create events marking the
%                             artefacts
%   S.D             - MEEG object or filename of M/EEG mat-file
%   S.badchanthresh - fraction of trials (or time) with artefacts above
%                     which a channel is declared as bad [default: 0.2]
%
%   S.append