-
Notifications
You must be signed in to change notification settings - Fork 3
/
octave_importglobal.m
63 lines (63 loc) · 1.8 KB
/
octave_importglobal.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
% Function to import time series CSV files for COVID data. Written for the
% following specific files:
% - 'time_series_covid19_confirmed_global.csv'
% - 'time_series_covid19_deaths_global.csv'
% from:
% 'https://github.com/CSSEGISandData/COVID-19/raw/master/csse_covid_19_data/csse_covid_19_time_series/'
%__________________________________________________________________________
# Copyright (C) 2021 Embecosm Limited
# Contributor: William Jones <william.jones@embecosm.com>
# SPDX-License-Identifier: GPL-2.0
function out = octave_importglobal(filename, delim)
if (nargin < 2)
delim = ',';
end
lines = textread(filename, '%s', "delimiter", "\n");
data = [];
textdata = {};
for i = 1:length(lines)
line = lines{i,1};
data_counter = 1;
text_counter = 1;
quote_open = 0;
buffer = '';
for j = 1:(length(line)+1)
if(j <= length(line))
current_char = line(j);
else
current_char = delim;
end
if(current_char == ("\""))
quote_open = !quote_open;
continue;
end
if((current_char == delim)&&!quote_open)
if (i == 1)
textdata{i, text_counter} = buffer;
text_counter = text_counter + 1;
else
if text_counter < 3
textdata{i, text_counter} = buffer;
text_counter = text_counter + 1;
else
if(isempty(buffer))
data(i-1, data_counter) = NaN;
else
try
data(i-1, data_counter) = str2num(buffer);
catch
disp(buffer);
end
end
data_counter = data_counter + 1;
end
end
buffer = '';
else
buffer = [buffer, current_char];
end
end
end
out.data = data;
out.textdata = textdata;
end