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_headplot.m
    path of file: ./repos/bspm/thirdparty/spm12/toolbox/MEEGtools
    
    the code of the file until where you have to start completion: function spm_eeg_headplot(Y, D, H)
% SPM interface to headplot function from EEGLAB
% FORMAT spm_eeg_headplot(Y, D, H)
%
%         Y - data vector
%         D - M/EEG object
%         H - (optional) axes handle
%
%__________________________________________________________________________
% Copyright (C) 2015-2016 Wellcome Trust Centre for Neuroimaging

% $Id: spm_eeg_headplot.m 6669 2016-01-11 15:51:06Z guillaume $

if ~exist('headplot', 'file')
    error('This functionality requires EEGLAB http://sccn.ucsd.edu/eeglab/');
end

if ~isfield(D, 'inv')
    error('Please define a forward model for 3D plotting');
end

try
    val = D.val;
catch
    val = 1;
end

eegind = 0;
for m = 1:numel(D.inv{val}.forward)
    if strncmp('EEG', D.inv{val}.forward(m).modality, 3)
        eegind = m;

    
function spm_eeg_headplot(Y, D, H)
% SPM interface to headplot function from EEGLAB
% FORMAT spm_eeg_headplot(Y, D, H)
%
%         Y - data vector
%         D - M/EEG object
%         H - (optional) axes handle
%
%__________________________________________________________________________
% Copyright (C) 2015-2016 Wellcome Trust Centre for Neuroimaging

% $Id: spm_eeg_headplot.m 6669 2016-01-11 15:51:06Z guillaume $

if ~exist('headplot', 'file')
    error('This functionality requires EEGLAB http://sccn.ucsd.edu/eeglab/');
end

if ~isfield(D, 'inv')
    error('Please define a forward model for 3D plotting');
end

try
    val = D.val;
catch
    val = 1;
end

eegind = 0;
for m = 1:numel(D.inv{val}.forward)
    if strncmp('EEG', D.inv{val}.forward(m).modality, 3)
        eegind = m;
    end
end

if ~eegind
    error('Only EEG modality is supported for 3D plotting.');
end

if isfield(D, 'spline_file')
    spline_file = D.spline_file;
else
    
    sens = D.inv{val}.datareg(eegind).sensors;
    
    elocs = [];
    for i = 1:numel(sens.label)
        elocs(i).labels = sens.label{i};
        elocs(i).X = sens.chanpos(i, 1);
        elocs(i).Y = sens.chanpos(i, 2);
        elocs(i).Z = sens.chanpos(i, 3);
    end
    
    spline_file = spm_file(fullfile(D), 'prefix', 'spline_', 'ext', 'spl');
    
    headplot('setup', elocs, spline_file);
    
    D.spline_file = spline_file;save(D);
end