Robochameleon  v1.0
paramParser.m
Go to the documentation of this file.
1 
2 
3 function out = paramParser(in, varargin)
4 
5 %% String -> Structure
6 if ischar(in)
7  % Strip path and extension
8  [~,name] = fileparts(in);
9 
10  %Strip text prefix
11  name = strsplit(name, '__');
12  if numel(name) <= 2
13  name = name{numel(name)};
14  else
15  robolog('Multiple text prefixes detected. Name is bad formatted', 'ERR');
16  end
17 
18  % Gets the iteration that is the number after the last separators (if present) (..._1)
19  regex= 'w*[_\s]\d+'; % Possible separators: _ \s(space)
20  iteration = regexp(name,regex,'match');
21  if ~isempty(iteration)
22  iteration = iteration{end};
23  it = str2double(iteration(2:end));
24  name = regexprep(name,regex,''); % Strip iteration from string
25  else
26  it = 0;
27  end
28  param.iteration=it;
29 
30  % The string can be formatted as follow to support substructures:
31  % 'P10_fiber[Length=100_D=17]_EDFA[Gain=10]_Length=10_10'
32 
33  % We first select the subgroups delimited by []
34  paramGroups = regexp(name,'[^_\s]+\[[^\]]+\]','match');
35  % and we add a last group with the ungrouped parameters
36  paramGroups{end+1} = regexprep(name,'_[^_\s]+\[[^\]]+\]','');
37 
38  for grp=paramGroups
39  [m, tks] = regexp(grp,'([^_\s]+)\[([^\]]+)\]','match', 'tokens');
40  subField = '';
41  if ~isempty(m{1})
42  % If the current group is a real group (and not the ungrouped group)
43  tokens = tks{1}{1};
44  keyValuePairs = tokens{2};
45  subField = tokens{1};
46  % Gets the parameters. Possible separators of the pairs are: _ space
47  strpairs = strsplit(keyValuePairs,{'_',' '});
48  else
49  strpairs = strsplit(grp{1},{'_',' '});
50  end
51 
52  % Gets the parameter name and value. Possible separators of the
53  % key/value are: = : nothing(for numerical value only)
54  regex = '-?(\d*\.)?\d+(e-?\d+)*'; % Regexp to match a number can be integer/float,
55  % positive/negative and in scientific notation
56  for i=1:length(strpairs)
57  try
58  % First try to assume we have a numerical parameter
59  num = regexp(strpairs{i},regex, 'match');
60  num = num{1};
61  rest = strsplit(strpairs{i},num);
62  key = rest{1}; % Strip suffix to the number (like dBm)
63  % Gets rid of separators ('=' and ':')
64  key = regexprep(key, '[:=]', '');
65  if ~isempty(subField)
66  param.(subField).(key) = str2double(num);
67  else
68  param.(key) = str2double(num);
69  end
70  catch
71  % If the previous fails, let's assume we have a string parameter
72  try
73  KeyValue = strsplit(strpairs{i}, {'=', ':'});
74  fieldName = KeyValue{1};
75  value = KeyValue{2};
76  if ~isempty(subField)
77  param.(subField).(fieldName) = value;
78  else
79  param.(fieldName) = value;
80  end
81  catch
82  error('Params were not well separated in the sring in file: %s.', in);
83  end
84  end
85  end
86  end
87  out = param;
88 
89 
90 %% Structure -> String
91 elseif isstruct(in)
92  if ~isempty(varargin)
93  prefix = varargin{1};
94  if ~ischar(prefix)
95  robolog('The prefix should be a string.', 'ERR');
96  end
97  end
98 
99  param = in;
100  str = '';
101  fields = fieldnames(param);
102  iteration=0;
103  for i=1:length(fields)
104  if strcmp(fields{i},'iteration')
105  iteration=param.iteration;
106  elseif strcmp(fields{i},'iter')
107  iteration=param.iter;
108  elseif strcmp(fields{i},'it')
109  iteration=param.it;
110  else
111  str = [str fields{i} '=' value2str(param.(fields{i})) '_'];
112  end
113  end
114  if iteration
115  str = [str num2str(iteration)];
116  else
117  str = str(1:end-1);
118  end
119  if ~isempty(prefix)
120  out = [prefix '__' str];
121  else
122  out = str;
123  end
124 else
125  error('Give me a structure or a string')
126 end
127 end
128 
129 function strValue = value2str(value)
130  strValue = num2str(value);
131  % If it's a decimal, remove leading zeros and use scientific notation
132  if strfind(strValue, '.')
133  vals = strsplit(strValue, '.');
134  if vals{1} == '0'
135  deci = vals{2};
136  i = 1;
137  while deci(i) == '0'
138  i = i + 1;
139  end
140  deci = deci(i:end);
141  if length(deci) > 1
142  strValue = sprintf('%s.%se-%d', deci(1), deci(2:end), i);
143  else
144  strValue = sprintf('%se-%d', deci(1), i);
145  end
146  end
147  return
148  end
149  % Remove trailing zeros
150  i = 0;
151  while strValue(end-i) == '0'
152  i = i + 1;
153  end
154  if i > 0
155  nonZeroPart = strValue(1:end-i);
156  if length(nonZeroPart) > 1
157  strValue = sprintf('%s.%se%d', nonZeroPart(1), nonZeroPart(2:end), i);
158  else
159  strValue = sprintf('%se%d', nonZeroPart, i);
160  end
161  end
162 end
function paramParser(in in, in varargin)
Translates structures of parameters into strings and strings into structrues.
function robolog(in msg, in varargin)
This function allows the user to print log messages in a standard way.