function replaceheader(inputfile, outputfile)

    % start timer
    tic
    
    % check input arguments
    if ( nargin < 2 ) || ( nargin > 3 )
        error('Incorrect number of input variables: syntax is filenumber = replaceheader(inputfile, outputfile)');
    elseif nargout > 1
        error('Incorrect number of output variables: syntax is filenumber = replaceheader(inputfile, outputfile)');
    end %if
    
    % check that input file exists
    try
        if exist([inputfile '.txt'], 'file') ~= 2 
            error(['cannot find input file ' inputfile '.txt']);
        end %if
    catch err
        disp(['error 634252 looking for input file ' inputfile '.txt']);
        rethrow(err);
    end %try
    
    % open file to read
    try
        inputfileid = fopen([inputfile '.txt'], 'r');
    catch err
        disp(['error 436374 opening input file ' inputfile '.txt']);
        rethrow(err);
    end %try

    % create file to write
    try
        outputfileid = fopen([outputfile '.txt'], 'w');
    catch err
        disp(['error 325785 opening output file ' outputfile '.txt']);
        rethrow(err);
    end %try
    
    % get first line
    try
        currentline = textscan(inputfileid,'%s',1,'delimiter','\n');
        currenttext = currentline{1}{1};
    catch err
        disp(['error 342567 reading header from input file ' inputfile '.txt']);
        rethrow(err);
    end %try

    % comment first line
    try
        line1 = ['# ' currenttext];
        fprintf(outputfileid, [line1 '\r\n']);
    catch err
        disp(['error 950243 writing header to output file ' outputfile '.txt']);
        rethrow(err);
    end %try

    % remove units for column headers
    try
        line2 = currenttext;
        line2 = regexprep(line2, '\[mm\]', '');
        line2 = regexprep(line2, '\[V/m\]', '');
        fprintf(outputfileid, [line2 '\r\n']);
    catch err
        disp(['error 823493 writing column headers to output file ' outputfile '.txt']);
        rethrow(err);
    end %try

    % skip line of dashes
    try
        textscan(inputfileid,'%s',1,'delimiter','\n');
    catch err
        disp(['error 342043 reading from input file ' inputfile '.txt']);
        rethrow(err);
    end %try

%     % copy data
%     try
%         disp('converting file...');
%         % first line
%         line = 1;
%         currentline = textscan(inputfileid,'%s',1,'delimiter','\n');
%         while (~feof(inputfileid)) % repeat until end of file
%             try
%                 % get text from current line
%                 currenttext = currentline{1}{1};
%                 % save to output file
%                 fprintf(outputfileid, [currenttext '\r\n']);
%                 % display progress
%                 if mod(line, 200000) == 0
%                     disp(['line ' num2str(line)]);
%                 end %if
%                 % next line
%                 line = line + 1;
%                 currentline = textscan(inputfileid,'%s',1,'delimiter','\n');
%             catch
%                 disp(['error 3829447 at data line ' num2str(line) ':']);
%                 disp(currentline);
%             end %try            
%         end %while
%         % report completion
%         disp(['completed converting ' num2str(line-1) ' lines']);
%     catch 
%         disp(['error 038233 copying data to output file ' outputfile num2str(filenumber) '.txt']);
%         rethrow(lasterror)
%     end %try

    % read in data
    try
        data = textscan(inputfileid, '%n %n %n %n %n %n') ;
    catch err
        disp(['error 382492 reading from input file ' inputfile '.txt']);
        rethrow(err);
    end %try
    
    % close files
    try
        fclose(inputfileid);
    catch err
        disp(['error 124356 closing input file ' inputfile '.txt']);        
    end %try
    try
        fclose(outputfileid);
    catch err
        disp(['error 124356 closing output file ' outputfile '.txt']);        
    end %try
    
    % store data in array
    try
        % initialise matrix
        fieldmap = [];
        % store data in matrix
        for i = 1:6
            fieldmap(:,i) = data{i};
        end %for
    catch err
        disp('error 832845 saving data to field map matrix');
        rethrow(err);
    end %try
    
    % write matrix to output file
    try
        dlmwrite([outputfile '.txt'], fieldmap, '-append', 'delimiter','\t', 'newline','pc', 'precision',10);
    catch err
        disp(['error 249023 writing field map data to output file ' outputfile '.txt']);
        rethrow(err);
    end %try
    
    % stop timer
    toc

return