Repository navigation
Expand file tree
/
Copy pathpars2str.m
More file actions
52 lines (47 loc) · 1.57 KB
/
Copy pathpars2str.m
File metadata and controls
52 lines (47 loc) · 1.57 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
function pars_str = pars2str(pars)
% Convert a parameter variable or a parameter structure into a string
% of the following format: field1_val1_field2_val2... This is useful
% for appending parameter values used in a simulation to file names,
% when the parameters are stored in a struct.
% Accepted input pars field includes: string, numerals, and numeric
% vector.
% Mengchen Zhu
if isstruct(pars)
% Extract the field names and values
names = fieldnames(pars);
val = struct2cell(pars);
else
names = {inputname(1)};
val = {pars};
end
% Interlace field names with their values
pars_str = '';
for n = 1:length(val)
% convert to string
if isnumeric(val{n})
if (length(val{n}) == 1)
pars_str = [pars_str, names{n}, '_', num2str(val{n})];
else
% numeric array, store the first, the increment, and the last value.
pars_str = [pars_str, names{n}, '_', num2str(val{n}(1)), ...
'_', num2str(val{n}(2) - val{n}(1)), '_', ...
num2str(val{n}(end))];
end
elseif islogical(val{n})
switch val{n}
case true
val{n} = 'true';
case false
val{n} = 'false';
end
pars_str = [pars_str, names{n}, '_', val{n}];
else
pars_str = [pars_str, names{n}, '_', val{n}];
end
% Replace "." with "p" to avoid file accessing issue
pars_str = regexprep(pars_str, '\.','p');
% No trailing '_' if the last parameter
if n < length(val)
pars_str = [pars_str, '_'];
end
end