This repository was archived by the owner on Nov 20, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathcsv2struct.m
More file actions
78 lines (74 loc) · 2.04 KB
/
csv2struct.m
File metadata and controls
78 lines (74 loc) · 2.04 KB
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
function Out = csv2struct(filename)
%CSV2STRUCT reads Excel's files stored in .xls or .csv file formats and
% stores results as a struct with field names based on the header row.
%
% DESCRIPTION
% The Excel file is assumed to have a single header row. The output struct
% will have a field for each column and the field name will be based on the
% column name stored in the header.
%
% Unlike csvread, csv2struct is able to read files with both text and
% number fields and store data as fields of a struct. Likely works on
% Windows machines only.
%
% See also:
% MATLAB's csvread and xlsread functions
% xml_read from my xml_io_tools which creates struct out of xml files
%
% Test:
% file_str = ['Name,City,Age\n', ...
% 'A. Cox,San Francisco,66\n', ...
% 'A. Ramos,London,47\n', ...
% 'A. Satou,Tokyo,33\n', ...
% 'C. Marshall,San Francisco,36\n'];
% fileID = fopen('test.csv','w');
% fwrite(fileID,sprintf(file_str),'char');
% fclose(fileID);
% X = csv2struct('test.csv')
%
% Written by Jarek Tuszynski, Leidos, jaroslaw.w.tuszynski_at_leidos.com
% Code covered by BSD License
%% read xls file with a single header row
[~, ~, raw] = xlsread(filename);
nRow = size(raw,1);
nCol = size(raw,2);
header = raw(1,:); % Store header information
raw(1,:) = []; % Remove headed from the data
%% Split data into txt & num parts
num = [];
txt = [];
ColNumeric = true(1,nCol);
for c = 1:nCol
col = raw(:,c);
for r = 1:nRow-1
if(~isnumeric(col{r}) || isnan(col{r})),
ColNumeric(c) = false;
break;
end
end
if (ColNumeric(c)),
num = [num cell2mat(col)]; %#ok<*AGROW>
else
txt = [txt col];
end
end
clear raw
%% Create struct with fields derived from column names from header
iNum = 1;
iTxt = 1;
for c=1:nCol
if ischar(header{c})
name = strtrim(header{c});
name(name==' ') = '_';
name = matlab.lang.makeValidName(name);
else
name = char('A'-1+c);
end
if (ColNumeric(c))
Out.(name) = num(:,iNum);
iNum = iNum+1;
else
Out.(name) = txt(:,iTxt);
iTxt = iTxt+1;
end
end