str2cfg.m

来自「approximate reinforcement learning」· M 代码 · 共 49 行

M
49
字号
function cfg = str2cfg(str, keywords)% String configuration to structure%   CFG = STR2CFG(STR, KEYWORDS)% Parameters:%   str         - the configuration string%   keywords    - keywords to search in the string, will become structure fields%       Should be a cell array of strings, or a structure. In the second case the structure%       field names will be used as keywords.% Returns:%   cfg         - string configuration% % Binary, numeric or string values are supported. Fields which are present% on the string, but for which no value is assigned are treated as boolean% "true" values.% E.g.%   cfg = str2cfg('alpha beta=2.5 gamma=value', {'alpha', 'beta', 'gamma', 'delta'}) produces% cfg = alpha: 1        (boolean true)%        beta: 2.500%       gamma: 'value'%       delta: 0        (boolean false)if isstruct(keywords),      % obtain field names    keywords = fieldnames(keywords); end;slen = length(str);cfg = struct;for i = 1:length(keywords),    kw = keywords{i};    kwlen = length(kw);    ix = strfind(str, keywords{i});    if isempty(ix), continue; end;      % no setting found    ix = ix(1);                         % only first occurence matters    after = ix + kwlen;    % if no value given, interpreted as boolean true    if (after > slen) || (str(after) ~= '='),        cfg.(kw) = 1;        continue;    end;    % otherwise, parse after equal    val = strtok(str(after+1:end), ' ');    % convert to numeric if possible    if ~isempty(str2num(val)),        val = str2num(val);    end;    cfg.(kw) = val;end;

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?