Initial commit
[pftoolbox.git] / GUI / model_edit_states.m
blobd202137fe229d6c3148564c7b91bcd47d777fd21
1 function model_edit_states(action,varargin);\r
2 % Sorry, I didn't have enough time to write nice and documented GUI code.\r
3 \r
4 % Toolbox for nonlinear filtering.\r
5 % Copyright (C) 2005  Jakob Rosén <jakob.rosen@gmail.com>\r
6 %\r
7 % This program is free software; you can redistribute it and/or\r
8 % modify it under the terms of the GNU General Public License\r
9 % as published by the Free Software Foundation; either version 2\r
10 % of the License, or (at your option) any later version.\r
11 %\r
12 % This program is distributed in the hope that it will be useful,\r
13 % but WITHOUT ANY WARRANTY; without even the implied warranty of\r
14 % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
15 % GNU General Public License for more details.\r
16 %\r
17 % You should have received a copy of the GNU General Public License\r
18 % along with this program; if not, write to the Free Software\r
19 % Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.\r
21 myfont='default';\r
23 if strcmp(action,'init')\r
24         callback=varargin{2};\r
25         parentfig=varargin{1};\r
26         parenthandles=guidata(parentfig);\r
27         \r
28         xvars=parenthandles.xvars;\r
29         x0=parenthandles.x0;\r
30         states=parenthandles.states;\r
32         if length(xvars)~=states\r
33                 if isempty(xvars);\r
34                         % Generate a default vector\r
35                         xvars=cell(1,states);\r
36                         for i=1:states\r
37                                 xvars{i}=['x',num2str(i)];\r
38                         end             \r
39                 else\r
40                         msgbox('The number of elements in ''xvars'' does not match the number of states');\r
41                         return;\r
42                 end\r
43         end\r
45         if length(x0)~=states\r
46                 if isempty(x0);\r
47                         % Generate a default vector\r
48                         x0=zeros(states,1);\r
49                 else\r
50                         msgbox('The number of elements in ''x0'' does not match the number of states');\r
51                         return;\r
52                 end\r
53         end\r
55         hf=figure('Name','State editor');\r
57         uicontrol('Style','Pushbutton','String','Save','Units','Normalized','Position',[0.8,0.02,0.1,0.05],'Callback',['model_edit_states(''save'',',num2str(hf),')'],'Fontname',myfont);\r
58         uicontrol('Style','Pushbutton','String','Cancel','Units','Normalized','Position',[0.9,0.02,0.1,0.05],'Callback',['delete(',num2str(hf),')'],'Fontname',myfont);\r
60         uicontrol(hf,'Style','Text','String','State','Units','normalized','Position',[0.0 0.9 0.1 0.05],'HorizontalAlign','Left','Fontname',myfont);\r
61         uicontrol(hf,'Style','Text','String','Name','Units','normalized','Position',[0.15 0.9 0.3 0.05],'HorizontalAlign','Left','Fontname',myfont);\r
62         uicontrol(hf,'Style','Text','String','Initial value','Units','normalized','Position',[0.8 0.9 0.2 0.05],'HorizontalAlign','Left','Fontname',myfont);\r
64         uicontrol(hf,'Style','Frame','Units','normalized','Position',[0.0 0.88 1 0.003]);\r
66 %       xvars={'x1','x2','x3','x4'};\r
67 %       x0=[0 0 0 0];\r
68 %       states=4;\r
70         uicontrol(hf,'Style','Frame','Units','normalized','Position',[0.0 0.1 1 0.003]);\r
71         uicontrol(hf,'Style','Text','String','States:','Units','normalized','Position',[0.0 0.02 0.20 0.05],'HorizontalAlign','Left','Fontname',myfont);\r
72         handles=guihandles(hf);\r
73         handles.edit_statenumber=uicontrol(hf,'Style','Edit','String',num2str(states),'Units','normalized','Position',[0.2 0.02 0.1 0.05],'callback',['model_edit_states(''changenumber'',' ,num2str(hf), ')'],'Fontname',myfont);\r
75         handles.xvars=xvars;\r
76         handles.states=states;\r
77         handles.x0=x0;\r
78         handles.parentfig=parentfig;\r
79         handles.callback=callback;\r
80         guidata(hf,handles);    \r
82         model_edit_states('updatestates',hf);\r
84 elseif strcmp(action,'save')\r
86         hf=varargin{1};\r
87         handles=guidata(hf);\r
89         states=handles.states;\r
90         xvars=cell(1,states);\r
91         x0=zeros(states,1);\r
92         \r
93         for i=1:handles.states\r
94                 name_str=get(handles.(['name_',num2str(i)]),'String');\r
95                 init_str=get(handles.(['init_',num2str(i)]),'String');\r
96                 \r
97                 if isempty(name_str)||isempty(init_str)\r
98                         msgbox('Error! A field is empty');\r
99                         return;\r
100                 end\r
101                 \r
102                 x0(i)=str2num(init_str);\r
103                 xvars{i}=name_str;\r
104         end\r
105         \r
106         parentfig=handles.parentfig;\r
107         parenthandles=guidata(parentfig);\r
109         parenthandles.states=states;\r
110         parenthandles.x0=x0;\r
111         parenthandles.xvars=xvars;\r
113         guidata(parentfig,parenthandles);       % Save data\r
114         \r
115         eval(handles.callback);\r
116         close(hf)\r
118 elseif strcmp(action,'changenumber')\r
120         hf=varargin{1};\r
121         handles=guidata(hf);    \r
122         states_old=handles.states;\r
123         states=str2num(get(handles.edit_statenumber,'String'));\r
124         sdiff=states-states_old;\r
126         xvars=handles.xvars;\r
127         x0=handles.x0;\r
129         if sdiff>0\r
130                 % Add more states\r
131                 x0(end+1:states)=0;\r
132                 for i=states_old+1:states\r
133                         xvars{i}=['x',num2str(i)];\r
134                 end;\r
135         elseif sdiff<0\r
136                 % Delete some states\r
137                 x0=x0(1:states);\r
138                 xvars=xvars(1:states);\r
139         else\r
140                 % Nothing has changed. Do nothing.\r
141                 return;\r
142         end\r
144         if size(x0,2)>1\r
145                 % A row vector! Let's transpone it.\r
146                 x0=x0';\r
147         end;\r
148         \r
149         handles.xvars=xvars;\r
150         handles.states=states;\r
151         handles.x0=x0;\r
152         guidata(hf,handles);\r
154         model_edit_states('updatestates',hf);\r
156 elseif strcmp(action,'updatestates')\r
158         hf=varargin{1};\r
159         handles=guidata(hf);    \r
161         states=handles.states;\r
162         xvars=handles.xvars;\r
163         x0=handles.x0;\r
166         % Remove old controls\r
167         i=1;\r
168         while isfield(handles,['number_',num2str(i)])\r
169                 delete(handles.(['number_',num2str(i)]));\r
170                 delete(handles.(['name_',num2str(i)]));\r
171                 delete(handles.(['init_',num2str(i)]));\r
172                 handles=rmfield(handles,['number_',num2str(i)]);\r
173                 handles=rmfield(handles,['name_',num2str(i)]);\r
174                 handles=rmfield(handles,['init_',num2str(i)]);\r
175                 i=i+1;\r
176         end;    \r
178         postop=0.8;\r
179         space=0.08;\r
180         pos=0;\r
182         % Add new controls\r
183         for i=1:states\r
184                 str=xvars{i};\r
185                 x0_str=num2str(x0(i));\r
187                 handles.(['number_',num2str(i)])=uicontrol(hf,'Style','Text','String',num2str(i),'Units','normalized','Position',[0.0 postop-space*pos 0.1 0.05],'Fontname',myfont);\r
188                 handles.(['name_',num2str(i)])=uicontrol(hf,'Style','Edit','String',str,'Units','normalized','Position',[0.15 postop-space*pos 0.6 0.05],'Fontname',myfont);\r
189                 handles.(['init_',num2str(i)])=uicontrol(hf,'Style','Edit','String',x0_str,'Units','normalized','Position',[0.8 postop-space*pos 0.1 0.05],'Fontname',myfont);\r
190                 pos=pos+1;\r
191         end\r
193         guidata(hf,handles);\r
194 end\r