1 function model_edit_states(action,varargin);
\r
2 % Sorry, I didn't have enough time to write nice and documented GUI code.
\r
4 % Toolbox for nonlinear filtering.
\r
5 % Copyright (C) 2005 Jakob Rosén <jakob.rosen@gmail.com>
\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
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
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
23 if strcmp(action,'init')
\r
24 callback=varargin{2};
\r
25 parentfig=varargin{1};
\r
26 parenthandles=guidata(parentfig);
\r
28 xvars=parenthandles.xvars;
\r
29 x0=parenthandles.x0;
\r
30 states=parenthandles.states;
\r
32 if length(xvars)~=states
\r
34 % Generate a default vector
\r
35 xvars=cell(1,states);
\r
37 xvars{i}=['x',num2str(i)];
\r
40 msgbox('The number of elements in ''xvars'' does not match the number of states');
\r
45 if length(x0)~=states
\r
47 % Generate a default vector
\r
50 msgbox('The number of elements in ''x0'' does not match the number of states');
\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
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
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
87 handles=guidata(hf);
\r
89 states=handles.states;
\r
90 xvars=cell(1,states);
\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
97 if isempty(name_str)||isempty(init_str)
\r
98 msgbox('Error! A field is empty');
\r
102 x0(i)=str2num(init_str);
\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
115 eval(handles.callback);
\r
118 elseif strcmp(action,'changenumber')
\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
131 x0(end+1:states)=0;
\r
132 for i=states_old+1:states
\r
133 xvars{i}=['x',num2str(i)];
\r
136 % Delete some states
\r
138 xvars=xvars(1:states);
\r
140 % Nothing has changed. Do nothing.
\r
145 % A row vector! Let's transpone it.
\r
149 handles.xvars=xvars;
\r
150 handles.states=states;
\r
152 guidata(hf,handles);
\r
154 model_edit_states('updatestates',hf);
\r
156 elseif strcmp(action,'updatestates')
\r
159 handles=guidata(hf);
\r
161 states=handles.states;
\r
162 xvars=handles.xvars;
\r
166 % Remove old controls
\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
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
193 guidata(hf,handles);
\r