7 % Strip path and extension
8 [~,name] = fileparts(in);
11 name = strsplit(name,
'__');
13 name = name{numel(name)};
15 robolog(
'Multiple text prefixes detected. Name is bad formatted',
'ERR');
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 30 % The
string can be formatted as follow to support substructures:
31 %
'P10_fiber[Length=100_D=17]_EDFA[Gain=10]_Length=10_10' 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]+\[[^\]]+\]',
'');
39 [m, tks] = regexp(grp,
'([^_\s]+)\[([^\]]+)\]',
'match',
'tokens');
42 % If the current group is a real group (and not the ungrouped group)
44 keyValuePairs = tokens{2};
46 % Gets the parameters. Possible separators of the pairs are: _ space
47 strpairs = strsplit(keyValuePairs,{
'_',
' '});
49 strpairs = strsplit(grp{1},{
'_',
' '});
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)
58 % First
try to assume we have a numerical parameter
59 num = regexp(strpairs{i},regex,
'match');
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, '[:=]', '');
66 param.(subField).(key) = str2double(num);
68 param.(key) = str2double(num);
71 % If the previous fails, let's assume we have a
string parameter
73 KeyValue = strsplit(strpairs{i}, {
'=',
':'});
74 fieldName = KeyValue{1};
77 param.(subField).(fieldName) = value;
79 param.(fieldName) = value;
82 error('Params were not well separated in the sring in file: %s.', in);
90 %% Structure -> String
95 robolog('The prefix should be a
string.', 'ERR');
101 fields = fieldnames(param);
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')
111 str = [str fields{i}
'=' value2str(param.(fields{i}))
'_'];
115 str = [str num2str(iteration)];
120 out = [prefix '__' str];
125 error('Give me a structure or a
string')
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, '.');
142 strValue = sprintf(
'%s.%se-%d', deci(1), deci(2:end), i);
144 strValue = sprintf(
'%se-%d', deci(1), i);
149 % Remove trailing zeros
151 while strValue(end-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);
159 strValue = sprintf(
'%se%d', nonZeroPart, i);
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.