comparison m-toolbox/classes/@smodel/mergeFields.m @ 0:f0afece42f48

Import.
author Daniele Nicolodi <nicolodi@science.unitn.it>
date Wed, 23 Nov 2011 19:22:13 +0100
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:f0afece42f48
1 % MERGEFIELDS merges properties (name/values) of smodels
2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3 %
4 % DESCRIPTION: MERGEFIELDS merges properties (name/values) of smodels
5 % in preparation of dual operations
6 %
7 % CALL: mergeFields(mdl1, mdl2, mdl, name, value)
8 %
9 % PARAMETERS: op - MATLAB operation name
10 % opname - Name for displaying
11 % opsym - Operation symbol
12 % infoObj - minfo object
13 % pl - default plist
14 % fcnArgIn - Input argument list of the calling fcn.
15 % varNames - Variable names of the input
16 %
17 % VERSION: $Id: mergeFields.m,v 1.1 2011/05/10 20:40:53 mauro Exp $
18 %
19 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
20
21 %--------------------------------------------------------------------------
22 % Merge Fields and Their Values
23 %--------------------------------------------------------------------------
24 function varargout = mergeFields(varargin)
25
26 mdl1 = varargin{1};
27 mdl2 = varargin{2};
28 mdl = varargin{3};
29 name = varargin{4};
30 value = varargin{5};
31
32 switch lower(name)
33 case {'params', 'aliasnames', 'xvar'}
34 otherwise
35 error('Unknown field %s', name);
36 end
37
38 % Start appending all the entries: (name)
39 mdl.(name) = [mdl1.(name) mdl2.(name)];
40
41 % If dealing with xvar+trans, we need to expand the trans
42 % This will be discontinued in future releases
43 if strcmpi(value, 'trans') || strcmpi(value, 'xunits')
44 n_vals = numel(mdl1.(value));
45 n_xvar = numel(mdl1.(name));
46 if n_vals == 1 && n_vals ~= n_xvar
47 valvec1 = cell(1, n_xvar);
48 for jj = 1:n_xvar
49 valvec1(jj) = mdl1.(value);
50 end
51 mdl1.(value) = valvec1;
52 end
53 n_vals = numel(mdl2.(value));
54 n_xvar = numel(mdl2.(name));
55 if n_vals == 1 && n_vals ~= n_xvar
56 valvec2 = cell(1, n_xvar);
57 for jj = 1:n_xvar
58 valvec2(jj) = mdl2.(value);
59 end
60 mdl2.(value) = valvec2;
61 end
62 end
63
64 % Start appending all the entries: (values)
65 if isempty(mdl1.(value))
66 values1 = cell(size(mdl1.(name)));
67 else
68 values1 = mdl1.(value);
69 end
70 if isempty(mdl2.(value))
71 values2 = cell(size(mdl2.(name)));
72 else
73 values2 = mdl2.(value);
74 end
75 mdl.(value) = [values1 values2];
76
77 % Check if the (name) are overlapping
78 [c, i1, i2] = intersect(mdl1.(name), mdl2.(name));
79 if isempty(c)
80 % No overlap: nothing to do
81 else
82 % Some overlap: check the (value)
83 for kk = 1: numel(c)
84 % xunits are always a vector
85 if strcmpi(value, 'xunits')
86 val1 = values1(i1(kk));
87 val2 = values2(i2(kk));
88 else
89 val1 = values1{i1(kk)};
90 val2 = values2{i2(kk)};
91 end
92 prop_name = c(kk);
93 if ~isempty(val1)
94 % (value) is defined in 1st model
95 if ~isempty(val2)
96 % (value) is defined in 2nd model
97 if ~isequal(val1, val2)
98 % the (value) are different, give error
99 error('LTPDA:err:ContentMismatchMismatch', 'The [%s] fields do not match, because ''%s'' are different!', name, prop_name{:});
100 else
101 % the (value) are equal, nothing to do
102 end
103 else
104 % (value) is not defined in 2nd model, take value from 1st
105 % Take the (name) position in the cell-array
106 idx = strcmp(mdl.(name), prop_name);
107 mdl.(value)(idx) = {val1 val1};
108 end
109 else
110 % (value) is not defined in 1st model
111 if ~isempty(val2)
112 % (value) is defined in 2nd model, take value from 2nd
113 % Take the (name) position in the cell-array
114 idx = strcmp(mdl.(name), prop_name);
115 mdl.(value)(idx) = {val2 val2};
116 else
117 % (value) is not defined in 2nd model, nothing to do
118 end
119 end
120 end
121
122 % All identical. Go ahead and merge
123 [mdl.(name), idx1, idx2] = unique(mdl.(name));
124 mdl.(value) = mdl.(value)(idx1);
125 end
126
127 % Set output
128 varargout = {};
129 end