-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdosubstitution.m
36 lines (32 loc) · 1.06 KB
/
dosubstitution.m
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
%==========================================================================
%==========================================================================
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