function combinefieldmaps(inputfiles, outputfile)

    % function COMBINEFIELDMAPS(inputfiles, outputfile)
    %
    % field maps listed in inputfiles cell array are combined into
    % a single output file, with headers stripped for entry into gpt
    %
    % inputfiles must be a cell array of files and their starting 
    % z-locations e.g. for a matching section at z=0 and an rfq at z=21.8mm:
    %   inputfiles = {'matching.txt', 0; 'rfq.txt', 21.8};
    %
    % also note assumption of file format: columns x y z Ex Ey Ez 
    % as from cst
    %
    % if the final field map is to be used in gpt, all input field maps
    % must have the same x and y points. this can be achieved in cst
    % by limiting the exported field data to a subvolume with the same 
    % x and y values and exporting with the same x and y spacing
    
    % check input arguments
    if ( nargin < 2 ) || ( nargin > 2 )
        error('Incorrect number of input variables: syntax is combinefieldmaps(inputfiles, outputfile)');
    elseif nargout > 0
        error('Incorrect number of output variables: syntax is combinefieldmaps(inputfiles, outputfile)');
    end %if
    
    % check that input files exist
    try
        % loop through input files
        for i = 1:length(inputfiles(:,1))
            % get filename
            inputfile = inputfiles(i,1);
            inputfile = inputfile{1}; % convert from cell to string
            % check current file exists
            try
                if exist(inputfile, 'file') ~= 2 
                    error(['cannot find input file ' inputfile]);
                end %if
            catch err2
                disp(['error 634252 looking for input file ' inputfile]);
                rethrow(err2);
            end %try
        end %for
    catch err1
        disp('error 284930 checking input files from the follow array:');
        disp(inputfiles);
        rethrow(err1);
    end %try
    
    % write headers 
    % - open file
    try
        outputfileid = fopen(outputfile, 'w');
    catch err
        disp(['error 325785 opening output file ' outputfile]);
        rethrow(err);
    end %try
    % - write
    try
        fprintf(outputfileid,'x\ty\tz\tEx\tEy\tEz\tBx\tBy\tBz\r\n');
    catch err
        disp(['error 124822 writing headers to output file ' outputfile]);
        rethrow(err);
    end %try
    % - close file
    try
        fclose(outputfileid);
    catch err
        disp(['error 124356 closing output file ' outputfile]);        
    end %try

    % read data from each file and write to output file
    try
        % loop through input files
        for i = 1:length(inputfiles(:,1))
            % get filename
            inputfile = inputfiles(i,1);
            inputfile = inputfile{1}; % convert from cell to string
            % open file1 to read
            try
                inputfileid = fopen(inputfile, 'r');
            catch err
                disp(['error 436374 opening input file ' inputfile]);
                rethrow(err);
            end %try
            % skip header rows
            try
                textscan(inputfileid,'%s',2,'delimiter','\n');
            catch err
                disp(['error 295843 reading from input file ' inputfile]);
                rethrow(err);
            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]);
                rethrow(err);
            end %try
            % close file
            try
                fclose(inputfileid);
            catch err
                disp(['error 124356 closing input file ' inputfile]);        
            end %try

            % store data in array
            try
                % get z-offset
                offset = inputfiles(i,2);
                offset = offset{1}; % convert from cell to numeric
                % initialise matrix
                fieldmap = [];
                % store data in matrix
                for j = 1:2 % x, y
                    fieldmap(:,j) = data{j}.*1e-3;
                end %for
                for j = 3:3 % z
                    fieldmap(:,j) = data{j}.*1e-3 + offset.*1e-3;
                end %for
                for j = 4:6 % Ex, Ey, Ez
                    fieldmap(:,j) = data{j};
                end %for
                % add empty B-field data
                for j = 7:9 % Bx, By, Bz
                    fieldmap(:,j) = 0;
                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, 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
            
        end %for
    catch err1
        disp('error 729310 when reading from input files in array:');
        disp(inputfiles);
        rethrow(err1);
    end %try

return