-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstruct2csv.m
104 lines (95 loc) · 2.84 KB
/
struct2csv.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
function struct2csv(s,fn)
% STRUCT2CSV(s,fn)
%
% Output a structure to a comma delimited file with column headers
%
% s : any structure composed of one or more matrices and cell arrays
% fn : file name
%
% Given s:
%
% s.Alpha = { 'First', 'Second';
% 'Third', 'Fourth'};
%
% s.Beta = [[ 1, 2;
% 3, 4]];
%
% s.Gamma = { 1, 2;
% 3, 4};
%
% s.Epsln = [ abc;
% def;
% ghi];
%
% STRUCT2CSV(s,'any.csv') will produce a file 'any.csv' containing:
%
% "Alpha", , "Beta", ,"Gamma", , "Epsln",
% "First","Second", 1, 2, 1, 2, "abc",
% "Third","Fourth", 3, 4, 3, 4, "def",
% , , , , , , "ghi",
%
% v.0.9 - Rewrote most of the code, now accommodates a wider variety
% of structure children
%
% Written by James Slegers, james.slegers_at_gmail.com
% Covered by the BSD License
%
FID = fopen(fn,'w');
headers = fieldnames(s);
m = length(headers);
sz = zeros(m,2);
t = length(s);
for rr = 1:t
l = '';
for ii = 1:m
sz(ii,:) = size(s(rr).(headers{ii}));
if ischar(s(rr).(headers{ii}))
sz(ii,2) = 1;
end
l = [l,'"',headers{ii},'",',repmat(',',1,sz(ii,2)-1)];
end
l = [l,'\n'];
fprintf(FID,l);
n = max(sz(:,1));
for ii = 1:n
l = '';
for jj = 1:m
c = s(rr).(headers{jj});
str = '';
if sz(jj,1)<ii
str = repmat(',',1,sz(jj,2));
else
if isnumeric(c)
for kk = 1:sz(jj,2)
str = [str,num2str(c(ii,kk)),','];
end
elseif islogical(c)
for kk = 1:sz(jj,2)
str = [str,num2str(double(c(ii,kk))),','];
end
elseif ischar(c)
str = ['"',c(ii,:),'",'];
elseif iscell(c)
if isnumeric(c{1,1})
for kk = 1:sz(jj,2)
str = [str,num2str(c{ii,kk}),','];
end
elseif islogical(c{1,1})
for kk = 1:sz(jj,2)
str = [str,num2str(double(c{ii,kk})),','];
end
elseif ischar(c{1,1})
for kk = 1:sz(jj,2)
str = [str,'"',c{ii,kk},'",'];
end
end
end
end
l = [l,str];
end
l = [l,'\n'];
fprintf(FID,l);
end
fprintf(FID,'\n');
end
fclose(FID);