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: writeCTFhdm.m
    path of file: ./repos/bspm/thirdparty/spm12/external/ctf
    
    the code of the file until where you have to start completion: function writeCTFhdm(fileName,hdm);

%  Version 1.1   20 April 2007   Modified to make sure that clinical-0ise message and 
%                                creator software are not repeated.
%                                Test date.
%                22 March 2007.  Modified to write v6.0 .hdm files which have additional

    
function writeCTFhdm(fileName,hdm);

%  Version 1.1   20 April 2007   Modified to make sure that clinical-0ise message and 
%                                creator software are not repeated.
%                                Test date.
%                22 March 2007.  Modified to write v6.0 .hdm files which have additional
%                                fields in MultiSphere_Data.  Check that the .hdm version
%                                number is compatible with the fields included in
%                                MultiSphere_Data.
%  Creates a CTF-compatible head model file from structure hdm
%  Structure hdm is in the format produced by readCTFhdm.

%  The formal of hdm (Head Model) files is given in document "CTF MEG FIle Formats",
%  PN900-0088.  The head model files are text files in the "Config Reader" format.

%  The purpose is to allow MATLAB users to specify multiple-local spheres head models
%  in a formatthat is compatible with CTF analysis software (DipoleFit).  It also allows
%  users to transfer .hdm files between datasets.

%  Head Model File format is defined in document "CTF MEG FIle Formats', PN900-0088.

%  Inputs : fileName: Name of the .hdm file to create.  It must include the full path,
%                     including dataset specification and the extension .hdm.
%           hdm:  The structure containing the class information.  See D

%  Output: Head model file.

persistent printWarning
creatorSoftware='writeCTFhdm';
versionSoftware='v1.1';

if nargin==0
  fprintf('writeCTFhdm   Version 1.1   20 April 2007\n');
  fprintf(['\twriteCTFhdm(fileName,hdm) creates a CTF-format head model file',...
      ' from the fields of structure hdm.\n',...
      '\t\t\tfileName is the name of the new head model file including path and ',...
      'extension ''.hdm''.\n',...
      '\t\t\thdm is a structure with the format produced by readCTFhdm.\n',...
      '\t\t\tThe head model file format is described in document ',...
      '"CTF MEG File Formats", PN900-0088.\n\n']);
  return
elseif nargin~=2
  fprintf(['\nwriteCTFhdm:  %d input argument(s).  writeCTFhdm needs 2 arguments:\n',...
      '              fileName and structure containing the head model.\n\n'],nargin);
  return
elseif ~ischar(fileName) | isempty(fileName) | ~isstruct(hdm) | isempty(hdm)
  fprintf('writeCTFhdm:  Wrong argument types, or empty arguments.\n');
  whos fileName hdm;
  return
elseif ndims(fileName)>2 | min(size(fileName))>1
  fprintf('\nwriteCTFhdm: size(fileName)=[');fprintf(' %d',size(fileName));
  fprintf(']    Input fileName must be a character string.\n\n');
  return
elseif exist(fileName)==2
  fprintf('writeCTFhdm: File %s already exists.\n',fileName);
  return
elseif isempty(strfind(fileName,'.hdm'))
  fprintf('writeCTFhdm: fileName=%s does not have .hdm extension.\n',fileName);
  return
elseif ~isfield(hdm,'Model') | ...
    (~isfield(hdm,'MultiSphere_Data') & ~isfield(hdm,'MEG_Sphere'))
  fprintf(['writeCTFhdm: Structure hdm is missing fields Multisphere_Data, ',...
      'MEG_Sphere or Model.\n']);
  fprintf('             It cannot be a useful head model file.\n');
  hdm
  return
elseif isfield(hdm,'MEG_Sphere') & ~isfield(hdm,'MultiSphere_Data')
  fprintf(['writeCTFhdm: Creating a single-sphere model.  ',...
      'No multisphere data found in structure hdm.\n']);
elseif ~isfield(hdm,'MEG_Sphere') & isfield(hdm,'MultiSphere_Data')
  fprintf(['writeCTFhdm: Creating a multi-sphere model.  ',...
      'No single-sphere data found in structure hdm.\n']);
end

%  Check version numbers and 'MultiSphere_Data fields.
if isfield(hdm,'File_Info') & isfield(hdm,'MultiSphere_Data')
  versionText=hdm.File_Info.VERSION;
  kpt=strfind(versionText,'VERSION_');
  version=sscanf(versionText(kpt+8:length(versionText)),'%g');
  if version<=5.9
    if isfield(hdm.MultiSphere_Data,'HEADPOS') | isfield(hdm.MultiSphere_Data,'SURFACE_TYPE')
      fprintf('\n');
      fprintf('%s\n',['writeCTFhdm: .hdm VERSION=',num2str(version,'%0.1f'),...
          ', but fields HEADPOS and/or SURFACE_TYPE appear'],...
        '              in hdm.MultiSphere_data.  They should appear only in v6.0 and higher.');
      return
    end