-
Notifications
You must be signed in to change notification settings - Fork 0
/
write_config.m
97 lines (91 loc) · 2.17 KB
/
write_config.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
function [ ] = write_config( output_dir )
config = get_config();
fn = sprintf('%s/%s', output_dir, config('config_output_filename'));
mkdir_basename(fn);
f = fopen(fn,'a');
if f ~= -1
printval(f, config);
fclose(f);
else
error('Could not write config to output file [%s]', fn);
end
end
function [] = printval(f, val)
if isa(val, 'containers.Map')
printmap(f, val);
elseif isa(val, 'double')
fprintf(f, '%f', val);
elseif isa(val, 'char')
fprintf(f, '''%s''', val);
elseif isa(val, 'logical')
if val
fprintf(f, 'true');
else
fprintf(f, 'false');
end
elseif isa(val, 'cell')
printcell(f,val);
elseif isa(val, 'struct')
printstruct(f,val);
elseif ismatrix(val)
printmatrix(f,val);
else
error('Cannot print type [%s]', class(val));
end
end
function [] = printcell(f, val)
fprintf(f, '{');
% assume only 2d cells for config
[r,~] = size(val);
for j=1:r
row = val(j,:);
for k=1:numel(row)
printval(f, val{j,k});
end
if r > 1
fprintf(f, '\n');
end
end
fprintf(f, '}\n');
end
function [] = printmatrix(f,val)
% again, assume only 2d
if isa(val(1,1), 'char')
% assume string
fprintf(f, '"%s"', val);
else
fprintf(f, '[');
[r,~] = size(val);
for j=1:r
row = val(j,:);
for k=1:numel(row)
printval(f, val(j,k));
end
if r > 1
fprintf(f, '\n');
end
end
fprintf(f, ']\n');
end
end
function [] = printstruct(f, val)
fprintf(f, 'structure(');
fields = fieldnames(val);
for j=1:numel(fields)
fprintf(f, '"%s",', fields{j});
printval(f, val.(fields{j}));
fprintf(f, ',');
end
fprintf(f, ')\n');
end
function [] = printmap(f, val)
fprintf(f, '{');
keyset = keys(val);
for i=1:numel(keyset)
key = keyset{i};
fprintf(f, '"%s": ', key);
printval(f, val(key));
fprintf(f, '\n');
end
fprintf(f, '}');
end