-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathstruct2csv.m
More file actions
77 lines (66 loc) · 1.95 KB
/
Copy pathstruct2csv.m
File metadata and controls
77 lines (66 loc) · 1.95 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
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
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);