0001 function data_st = ensemble_csv2datastruct(in_st,params)
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022 if nargin < 2
0023 if ischar(in_st)
0024 fname = in_st;
0025 else
0026 error('%s: argument must be string if only one argument is passed in', mfilename)
0027 end
0028 params = struct;
0029 elseif nargin == 2
0030 if ~isfield(params, 'fname')
0031 if ~isempty(in_st)
0032 fname = in_st;
0033 else
0034 error('%s: name of file to load must be provided in 1st argument or as the fname field in 2nd argument', mfilename)
0035 end
0036 else
0037 fname = params.fname;
0038 end
0039 end
0040
0041 data_st = ensemble_init_data_struct;
0042
0043 if ~exist(fname,'file')
0044 error('%s: File %s does not exist', mfilename, fname)
0045 end
0046 [~,fstub] = fileparts(fname);
0047 data_st.name = fstub;
0048 data_st.type = 'csvfile';
0049
0050
0051 fid = fopen(fname,'rt');
0052
0053
0054 cl = fgetl(fid);
0055
0056
0057 vars = regexp(cl,',','split');
0058 nvars = length(vars);
0059
0060
0061 vars = regexprep(vars,'\s+','_');
0062 data_st.vars = vars;
0063
0064
0065 data = cell(1,nvars);
0066 numRows = 0;
0067
0068 if isfield(params, 'USE_MATCH_HEURISTIC')
0069 USE_MATCH_HEURISTIC = params.USE_MATCH_HEURISTIC;
0070 else
0071 USE_MATCH_HEURISTIC = 0;
0072 end
0073
0074 while ~feof(fid)
0075 cl = fgetl(fid);
0076 numRows = numRows+1;
0077
0078
0079
0080
0081
0082 if USE_MATCH_HEURISTIC
0083 pat = '(".+")|([^,]*)';
0084 tokens = regexp(cl,pat,'match');
0085 else
0086
0087
0088
0089
0090 pat = '(?!(?=,[\w\d\s]+")),';
0091 tokens = regexp(cl,pat,'split');
0092
0093 end
0094
0095
0096 tokens = regexprep(tokens,'"','');
0097 ntok = length(tokens);
0098
0099
0100 if ntok ~= nvars
0101
0102
0103 if ntok == (nvars-1) && strcmp(',',cl(end))
0104 tokens{end+1} = ' ';
0105 ntok = length(tokens);
0106 else
0107 error('%s: Number of tokens (%d) does not match number of variables (%d)', mfilename, ntok, nvars)
0108 end
0109 end
0110
0111
0112 if numRows == 1
0113 varIsNumeric = ~isnan(str2double(tokens));
0114 end
0115
0116 for itok = 1:ntok
0117
0118
0119 if varIsNumeric(itok)
0120 data{itok}(numRows,1) = str2double(tokens{itok});
0121 else
0122 data{itok}{numRows,1} = tokens{itok};
0123 end
0124
0125 end
0126
0127 end
0128
0129
0130 fclose(fid);
0131
0132
0133 ncols = size(data,2);
0134 if nvars ~= ncols
0135 error('%s: Number of columns (%d) does not match number of variables (%d)',mfilename, ncols, nvars);
0136 end
0137
0138 data_st.data = data;
0139 return