Skip to content

Commit

Permalink
dictionary substitutions
Browse files Browse the repository at this point in the history
  • Loading branch information
jirkacigler committed Jun 5, 2012
1 parent ad97fb2 commit 6c0eaf7
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 2 deletions.
12 changes: 10 additions & 2 deletions ReadYaml.m
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@
% exception and halts the process
% makeords ... Determines whether to convert cell array to
% ordinary matrix whenever possible (1).
function result = ReadYaml(filename, nosuchfileaction, makeords, treatasdata)
% dictionary ... Dictionary of of labels that will be replaced,
% struct is expected
function result = ReadYaml(filename, nosuchfileaction, makeords, treatasdata, dictionary)
if ~exist('nosuchfileaction','var')
nosuchfileaction = 0;
end;
Expand All @@ -41,6 +43,8 @@
if ~ismember(treatasdata,[0,1])
error('treatasdata parameter must be 0,1 or missing.');
end;


ry = ReadYamlRaw(filename, 0, nosuchfileaction, treatasdata);
ry = deflateimports(ry);
if iscell(ry) && ...
Expand All @@ -52,7 +56,11 @@
end;
ry = mergeimports(ry);
ry = doinheritance(ry);
ry = makematrices(ry, makeords);
ry = makematrices(ry, makeords);
if exist('dictionary','var')
ry = dosubstitution(ry, dictionary);
end;

result = ry;
clear global nsfe;
end
36 changes: 36 additions & 0 deletions dosubstitution.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
%==========================================================================

%==========================================================================
function result = dosubstitution(r, dictionary)
if ~exist('dictionary','var')
dictionary = {};
end;
result = recurse(r, 0, dictionary);
end

function result = recurse(data, level, dictionary)
if iscell(data) && ~ismymatrix(data)
result = iter_cell(data, level, dictionary);
elseif isstruct(data)
result = iter_struct(data, level, dictionary);
elseif ischar(data) && isfield(dictionary, data)
result = dictionary.(data);
else
result = data;
end;
end

function result = iter_cell(data, level, dictionary)
result = {};
for i = 1:length(data)
result{i} = recurse(data{i}, level + 1, dictionary);
end;
end

function result = iter_struct(data, level, dictionary)
result = data;
for i = fields(data)'
fld = char(i);
result.(fld) = recurse(data.(fld), level + 1, dictionary);
end;
end

0 comments on commit 6c0eaf7

Please sign in to comment.