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_split_conditions.m
    path of file: ./repos/bspm/thirdparty/spm12/toolbox/MEEGtools
    
    the code of the file until where you have to start completion: function D = spm_eeg_split_conditions(S)
% Splits a file into different conditions in order to facilitate TF
% processing. The idea is to create several smaller files, run TF, then
% aveage within the condition files using spm_eeg_average_tf and lastly,
% merge again.
% FORMAT D = spm_eeg_split_conditions(S)
%
% S        - optional input struct
% (optional) fields of S:
% D        - MEEG object or filename of M/EEG mat-file with epoched data
%
% Output:
% D        - MEEG object (also written on disk)
%

    
function D = spm_eeg_split_conditions(S)
% Splits a file into different conditions in order to facilitate TF
% processing. The idea is to create several smaller files, run TF, then
% aveage within the condition files using spm_eeg_average_tf and lastly,
% merge again.
% FORMAT D = spm_eeg_split_conditions(S)
%
% S        - optional input struct
% (optional) fields of S:
% D        - MEEG object or filename of M/EEG mat-file with epoched data
%
% Output:
% D        - MEEG object (also written on disk)
%
% The function also physically removes bad trials.
%
%__________________________________________________________________________
% Copyright (C) 2008 Wellcome Trust Centre for Neuroimaging

% Dominik R Bach
% based on spm_eeg_remove_bad_trials

SVNrev = '$Rev: 6374 $';

%-Startup
%--------------------------------------------------------------------------
spm('FnBanner', mfilename, SVNrev);
spm('FigName','Remove bad trials'); spm('Pointer','Watch');

%-Get MEEG object
%--------------------------------------------------------------------------
try
    D = S.D;
catch
    [D, sts] = spm_select(1, 'mat', 'Select M/EEG mat file');
    if ~sts, D = []; return; end