xxlibReadCsvHeader

PURPOSE ^

xxlibReadCsvHeader - reads the header of comma separated value (csv) file

SYNOPSIS ^

function [names] = xxlibReadCsvHeader(varargin)

DESCRIPTION ^

 xxlibReadCsvHeader - reads the header of comma separated value (csv) file

 Syntax: 
   [names] = xxlibReadCsvHeader(fileName, sepChar)
  
 Inputs:
   filename = full path of csv file (required)
   sepChar  = separating character of csv file (optional) e.g ';' (default) OR ','
 
 Output:
   names  =  list of strings on first row 
    
 See also: xxlibReadCsv

 Author: Controllab Products B.V.
 email: info@controllab.nl
 Website: http://www.controllab.nl
 September 2013

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function [names] = xxlibReadCsvHeader(varargin)
0002 % xxlibReadCsvHeader - reads the header of comma separated value (csv) file
0003 %
0004 % Syntax:
0005 %   [names] = xxlibReadCsvHeader(fileName, sepChar)
0006 %
0007 % Inputs:
0008 %   filename = full path of csv file (required)
0009 %   sepChar  = separating character of csv file (optional) e.g ';' (default) OR ','
0010 %
0011 % Output:
0012 %   names  =  list of strings on first row
0013 %
0014 % See also: xxlibReadCsv
0015 %
0016 % Author: Controllab Products B.V.
0017 % email: info@controllab.nl
0018 % Website: http://www.controllab.nl
0019 % September 2013
0020 
0021 %------------- BEGIN CODE --------------
0022     %check input arguments
0023     if length(varargin)==0
0024         error('Not enough input arguments.');
0025     elseif length(varargin)>2
0026         error('Too many input arguments.');
0027     end
0028 
0029     if ~ischar(varargin{1})
0030         error('Input argument 1 should be a file name.');
0031     else
0032         filename = varargin{1};
0033     end
0034     
0035     if length(varargin)==2
0036         sepChar = varargin{2};
0037     else
0038         sepChar = ';';
0039     end
0040 
0041     header = '';
0042 
0043     fid = fopen(filename,'r');
0044         if (fid < 0) 
0045             fprintf('Error: could not open file\n');
0046         return;
0047     end
0048 
0049     if ~feof(fid),
0050         header = fgetl(fid);
0051     end
0052 
0053     names1 = strsplit(header, sepChar);
0054 
0055     % now the names can contain double quotes.
0056     % so try to remove them
0057     nrNames = length(names1);
0058     i = 1;
0059     counter = 1;
0060     while( i<= nrNames)
0061     
0062         % does it contain a value or a name
0063         if ~isempty(str2num(names1{i}))
0064             % value found, so empty name array
0065             names = []
0066             break;
0067         end
0068 
0069         % remove all the quotes from the header string
0070         % this was single quote removal... names1 {i} = strrep(names1{i},'''','');
0071 
0072         % does the name start with a double quote...
0073         if names1{i}(1) == '"'
0074         
0075             % does the name end with a double quote?
0076             if names1{i}(length(names1{i})) == '"'
0077                 % full name, just remove the quotes
0078                 names {counter} = strrep(names1{i},'"','');
0079             else
0080                 
0081                 names1 {i} = strrep(names1{i},'"','');
0082                 names{counter} = names1{i};
0083                 
0084                 i = i+1;
0085                 names1 {i} = strrep(names1{i},'"','');
0086                 
0087                 % add the sepChar again, assuming that was the reason of the split
0088                 names{counter} = strcat(names{counter}, sepChar);
0089                 
0090                 % add the next name
0091                 names{counter} = strcat(names{counter}, names1{i});
0092             end
0093         else
0094             names{counter} = names1{i};
0095         end
0096         counter = counter + 1;
0097         i = i + 1;
0098     end
0099   fclose(fid);
0100   
0101 end
0102 %------------- END OF CODE --------------

Generated on Sun 10-Dec-2017 19:24:51