Initial commit
[pftoolbox.git] / models / @dnl / set.m
blob4faf8176a63bd017f7df76b34e3b468d48b49c76
1 function objout = set(obj,varargin)\r
2 % Sets the properties of an object.\r
3 %\r
4 % set(obj, 'propertyname', value) sets the property 'propertyname' of the object 'obj'\r
5 % to the value 'value'. \r
6 %\r
7 % An equivalent syntax is obj.propertyname = value.\r
8 %\r
9 % set(obj, 'property1', value1, 'property2', value2, ...) sets multiple property values.\r
10 % set(obj, 'property') displays legitimate values for the specified property of 'obj'.\r
11 % set(obj) displays all properties of 'obj' and their admissible values.\r
12 %\r
13 % If an output argument is specified, the modified object will be assigned to this and\r
14 % no modifications will be done to the input object.\r
15 %\r
16 % Type 'props(obj)' for more details on the properties of the object 'obj'.\r
18 % Toolbox for nonlinear filtering.\r
19 % Copyright (C) 2005  Jakob Rosén <jakob.rosen@gmail.com>\r
20 %\r
21 % This program is free software; you can redistribute it and/or\r
22 % modify it under the terms of the GNU General Public License\r
23 % as published by the Free Software Foundation; either version 2\r
24 % of the License, or (at your option) any later version.\r
25 %\r
26 % This program is distributed in the hope that it will be useful,\r
27 % but WITHOUT ANY WARRANTY; without even the implied warranty of\r
28 % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
29 % GNU General Public License for more details.\r
30 %\r
31 % You should have received a copy of the GNU General Public License\r
32 % along with this program; if not, write to the Free Software\r
33 % Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.\r
35 if nargin==1\r
36         % Only 'obj' is supplied. Display the list of properties.\r
37         displayprops(obj);\r
38 elseif nargin==2;\r
39         % A parameter was given, but no value. Display information about the property.\r
40         prop_name = varargin{1};\r
41         [props,rprops]=pnames;\r
42         if findcstr(props,prop_name)||findcstr(rprops,prop_name)\r
43                 disp(pformat(get(obj,prop_name)));\r
44         else\r
45                 error('Unknown property');\r
46         end\r
47 else\r
48         if isempty(inputname(1))&&nargout<1\r
49                 error('The first argument must be a named variable if no output argument is given.')\r
50         elseif rem(nargin-1,2)~=0,\r
51                 error('Property/value pairs must come in even number.')\r
52         end\r
54         property_argin = varargin;\r
55         while length(property_argin) >= 2,\r
56                 prop_name = property_argin{1};\r
57                 v = property_argin{2};\r
58                 property_argin = property_argin(3:end);\r
59                 switch prop_name\r
61                 % <custom>\r
62                 % Here we handle properties that need custom treatment (or need to be \r
63                 % processed really fast)\r
65                 case 'x0'\r
66                         if size(v,2)>1\r
67                                 % A row vector! Transpone it\r
68                                 obj.x0=v';\r
69                         else\r
70                                 obj.x0=v;\r
71                         end\r
72                 case 'w'\r
73                         obj.w = initnoise(v);\r
74                 case 'e'\r
75                         obj.e = initnoise(v);\r
76                 case 'p0'\r
77                         obj.p0 = initnoise(v);\r
78                 case 'fgradx'\r
79                         f=obj.f;\r
80                         obj.f = set(f,'gradx',v);\r
81                 case 'fgradw'\r
82                         f=obj.f;\r
83                         obj.f = set(f,'gradw',v);\r
84                 case 'hgradx'\r
85                         h=obj.h;\r
86                         h = set(h,'gradx',v);\r
87                         obj.h=h;\r
88                 case 'hgrade'\r
89                         h=obj.h;\r
90                         obj.h = set(h,'gradw',v);\r
91                 case {'f','gw','gu','h','hu'}\r
92                         if strcmp(prop_name,'f')||strcmp(prop_name,'h')\r
93                                 % f(x,t) and h(x,t) depends on x by default\r
94                                 evalvar=1;\r
95                         else\r
96                                 % the rest are independent of x (by default)\r
97                                 evalvar=0;\r
98                         end\r
99                         % Get the previous model.\r
100                         exprobj=obj.(prop_name);\r
101                         xvars=obj.xvars;\r
102                         if isempty(xvars)\r
103                                 % The xvars property of the model is empty.\r
104                                 % Use the xvars of the previous object.\r
105                                 xvars=get(exprobj,'xvars');\r
106                         end\r
107                         % Initialize new data object.\r
108                         exprobj=initdata(v,xvars,{},{},evalvar);\r
109                         % Store the new object within the model.\r
110                         obj.(prop_name)=exprobj;\r
111                         \r
112                 % </custom>\r
114                 otherwise\r
115                         [props,rprops]=pnames;\r
116                         if findcstr(props,prop_name)\r
117                                 eval(strcat('obj.',prop_name,'=v;'));\r
118                         elseif findcstr(rprops,prop_name)\r
119                                 error([prop_name,' is a read-only property'])\r
120                         else\r
121                                 error([prop_name,' is not a valid property'])\r
122                         end\r
123                 end\r
124         end\r
126         if nargout==0\r
127                 % No output variable was specified by the user.\r
128                 % We'll do the assignment manually in the caller's workspace.\r
129                 objname = inputname(1);\r
130                 assignin('caller',objname,obj)\r
131         else\r
132                 objout=obj;\r
133         end;\r
134 end\r