diff --git a/matlab/load_csv_file_data.m b/matlab/load_csv_file_data.m index 72e5857c66b065d3ebe20ae240ea9025a11c123e..a66f7eea527f5112cb9a8889383cf67d1f60ad77 100644 --- a/matlab/load_csv_file_data.m +++ b/matlab/load_csv_file_data.m @@ -1,5 +1,5 @@ function [freq, init, data, varlist] = load_csv_file_data(file) - +%function [freq, init, data, varlist] = load_csv_file_data(file) % Loads data in a csv file. % % INPUTS @@ -16,7 +16,7 @@ function [freq, init, data, varlist] = load_csv_file_data(file) % names. Similarly, if the first column does not contain dates, then % freq will be 1 and init will be year 1. -% Copyright (C) 2012-2013 Dynare Team +% Copyright (C) 2012-2014 Dynare Team % % This file is part of Dynare. % @@ -41,28 +41,126 @@ varlist = []; assert(exist(file, 'file') == 2, ['load_csv_file_data: I can''t find file ' file '!']); if isoctave - if ~user_has_octave_forge_package('io') - error('The io package is required to read CSV files from Octave') - end - A = csv2cell(file); - [data, T, L] = parsecell(A); - withvars = L.numlimits(2,1) > L.txtlimits(2,1); - withtime = L.numlimits(1,1) > L.txtlimits(1,1); + % Workaround for #755 + try + fid = fopen(file, 'r'); + catch + error(['load_csv_file_data: I can''t find file ' file]) + end + firstline = fgetl(fid, 4097); + fclose(fid); + + if length(firstline) < 4097 + if ~user_has_octave_forge_package('io') + error('The io package is required to read CSV files from Octave'); + end + A = csv2cell(file); + [data, T, L] = parsecell(A); + withvars = L.numlimits(2,1) > L.txtlimits(2,1); + withtime = L.numlimits(1,1) > L.txtlimits(1,1); + else + fid = fopen(file, 'r'); + bfile = fread(fid); + fclose(fid); + + if isunix || ismac + newline_code = 10; + elseif ispc + newline_code = 13; + else + error('load_csv_file_data is not implemented for your OS'); + end + + % Get the positions of the end-of-line code + end_of_line_locations = find(bfile==newline_code); + if ispc && isempty(end_of_line_locations) + newline_code=10; + end_of_line_locations = find(bfile==newline_code); + end; + tmp = find(bfile==newline_code); + + % Get the number of lines in the file + ndx = length(tmp); + + % Create a cell of indices for each line + b = [1; end_of_line_locations+1]; + c = [end_of_line_locations-1; length(bfile)+1]; + b = b(1:end-1); + c = c(1:end-1); + linea = 1; + if withnames + % Get the first line of the csv file (names of the variables). + linee = char(transpose(bfile(b(linea):c(linea)))); + % Get the content of the first line and determine the number of variables and their names. + [B,C] = get_cells_id(linee,','); + if withtime + B = B(2:end); + C = C(2:end); + end + varlist = cell(length(B),1); + number_of_variables = length(varlist); + for i=1:number_of_variables + varlist(i) = {linee(B(i):C(i))}; + end + varlist = strtrim(varlist); + linea = linea+1; + end + + % Get following line (number 1 or 2 depending on withnames flag) + linee = char(transpose(bfile(b(linea):c(linea)))); + comma_locations = transpose(strfind(linee,',')); + B = 1; + C = comma_locations(1)-1; + if withtime + tmp = linee(B:C); + % Check the dates formatting + if isnumeric(tmp) && isint(tmp) + tmp = [num2str(tmp) 'Y']; + end + if ~isdate(tmp) + error('load_csv_file_data:: Formatting error. I can''t read the dates!') + end + init = dates(tmp); + freq = init.freq; + first = 2; + else + first = 1; + end + + if ~withnames + number_of_variables = length(tmp)-withtime; + end + + % Initialization of matrix data. + data = zeros(ndx,number_of_variables); + + % Populate data. + for linea = 1+withnames:ndx + linee = char(transpose(bfile(b(linea):c(linea)))); + [B,C] = get_cells_id(linee,','); + for i=first:length(B) + data(linea,i-withtime) = str2double(linee(B(i):C(i))); + end + end + + % Remove first line if withnames + data = data(1+withnames:ndx, :); + end else - A = importdata(file, ','); - if ~isstruct(A) - data = A; - T = {}; - withvars = 0; - withtime = 0; - else - data = A.data; - T = A.textdata; - % importdata() allows text only at the top and the left, so the following - % tests are sufficient. - withvars = size(T, 2) >= size(data, 2); - withtime = size(T, 1) >= size(data, 1); - end + A = importdata(file, ','); + if ~isstruct(A) + data = A; + T = {}; + withvars = 0; + withtime = 0; + else + data = A.data; + T = A.textdata; + % importdata() allows text only at the top and the left, so the following + % tests are sufficient. + withvars = size(T, 2) >= size(data, 2); + withtime = size(T, 1) >= size(data, 1); + end end if withvars