-
Notifications
You must be signed in to change notification settings - Fork 1
/
csvread_dataframe.m
57 lines (50 loc) · 1.89 KB
/
csvread_dataframe.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
function dat = csvread_dataframe(filename)
%CSVREAD_DATAFRAME (ps-utils): read CSV file outputted by R as a dataframe
% DAT = CSVREAD_DATAFRAME(FILENAME)
% DAT is a structure
%
% write dataframe like this:
% > write.table(nbSum$coefficients, '/tmp/t1.txt', sep=',')
%
% MH - http://github.com/histed/tools-mh
% first get header
fid = fopen(filename);
headertxt = fgetl(fid);
colnames = deblank_bothsides(strread(headertxt, '%q', 'delimiter', ','));
ncols = length(colnames)+1;
formatstr = repmat('%q',1,ncols);
% now read data
[d{1:ncols}] = textread(filename, ...
formatstr, ...
'delimiter', ',', ...
'headerlines', 1, ...
'emptyvalue', NaN);
% convert numeric fields to numeric and text to text
for iCol = 1:ncols
tFieldVal = d{iCol};
emptyIx = cellfun('isempty', tFieldVal);
numField = str2double(tFieldVal);
% is this really a numeric field?
textIx = isnan(numField) & ~emptyIx;
numIx = ~isnan(numField);
assert(all(numIx == (~textIx & ~emptyIx)), ...
'Bug: I made a wrong assumption about str2double and NaN results?');
numIx = find(numIx);
textIx = find(textIx);
if ~isempty(numIx) && isempty(textIx)
% numeric field
fieldVals{iCol} = numField;
elseif ( (~isempty(textIx) && isempty(numIx)) ... % text field
|| (isempty(textIx) && isempty(numIx)) ) % entire field empty
fieldVals{iCol} = deblank(tFieldVal); % TEXTREAD converts to string
% (remove trailing whitespace)
else
error(sprintf(['Both string and numeric values were found in ' ...
'index field: %s'], ...
colnames{iCol}));
end
end
% convert to a more raw structure
dat.colNames = colnames;
dat.rowNames = fieldVals(:,1);
dat.columns = fieldVals(:,2:end);