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: afni_niml_write.m
    path of file: ./repos/fieldtrip/external/afni
    
    the code of the file until where you have to start completion: function afni_niml_write(p,fn,format)
% simple function to write niml files in ASCII format
%
% AFNI_NIML_WRITE(P,FN) writes a NIML struct to file FN. P should be in the
% form as returned by AFNI_NIML_READ.
%
% The order of the arguments may be reversed. If FN is numeric, then it is
% assumed to be a file identifier. If P is omitted, output is written to
% stdout.
%
% This function requires a tree-like structure similar to the NIML format.
% To write a 'simple' struct, use AFNI_NIML_WRITESIMPLE.
%
% NNO Dec 2009 <n.oosterhof@bangor.ac.uk>

if nargin<3
    format='binary';
end

if nargin<2

    
function afni_niml_write(p,fn,format)
% simple function to write niml files in ASCII format
%
% AFNI_NIML_WRITE(P,FN) writes a NIML struct to file FN. P should be in the
% form as returned by AFNI_NIML_READ.
%
% The order of the arguments may be reversed. If FN is numeric, then it is
% assumed to be a file identifier. If P is omitted, output is written to
% stdout.
%
% This function requires a tree-like structure similar to the NIML format.
% To write a 'simple' struct, use AFNI_NIML_WRITESIMPLE.
%
% NNO Dec 2009 <n.oosterhof@bangor.ac.uk>

if nargin<3
    format='binary';
end

if nargin<2
    fn=1;
end

if isstruct(fn) || iscell(fn) % allow for wrong order of arguments; swap
    tmp=p;
    p=fn;
    fn=tmp;
end

% conversion to string
s=afni_niml_print(p, format);

% see if output is a file identifier
if isnumeric(fn) && round(fn)==fn
    fid=fn;
    fn=sprintf('FID %d',fid);
else
    fid=fopen(fn,'w');
end