1 function pfgui_action(action,hObject, 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
21 handles=guidata(hObject);
\r
23 if strcmp(action,'sim_radio_1')
\r
25 set(handles.sim_radio_1,'Value',1);
\r
26 set(handles.sim_radio_2,'Value',0);
\r
28 set(handles.sim_y_text,'Visible','off');
\r
29 set(handles.sim_y_edit,'Visible','off');
\r
30 set(handles.sim_y_size,'Visible','off');
\r
31 set(handles.sim_x_text,'Visible','off');
\r
32 set(handles.sim_x_edit,'Visible','off');
\r
33 set(handles.sim_x_size,'Visible','off');
\r
35 set(handles.sim_model,'Visible','on');
\r
36 set(handles.sim_steps_text,'Visible','on');
\r
37 set(handles.sim_steps,'Visible','on');
\r
39 handles.simulator_mode=true;
\r
40 guidata(hObject,handles);
\r
44 if strcmp(action,'sim_radio_2')
\r
45 set(handles.sim_radio_2,'Value',1);
\r
46 set(handles.sim_radio_1,'Value',0);
\r
48 set(handles.sim_model,'Visible','off');
\r
49 set(handles.sim_steps_text,'Visible','off');
\r
50 set(handles.sim_steps,'Visible','off');
\r
52 set(handles.sim_y_text,'Visible','on');
\r
53 set(handles.sim_y_edit,'Visible','on');
\r
54 set(handles.sim_y_size,'Visible','on');
\r
55 set(handles.sim_x_text,'Visible','on');
\r
56 set(handles.sim_x_edit,'Visible','on');
\r
57 set(handles.sim_x_size,'Visible','on');
\r
59 handles.simulator_mode=false;
\r
60 guidata(hObject,handles);
\r
64 if strcmp(action,'y_import')
\r
65 % Make this a shared function between x and y instead!
\r
66 y_edit_str=get(handles.sim_y_edit,'String');
\r
67 if isempty(y_edit_str)
\r
71 y=evalin('base',y_edit_str,'[]');
\r
73 % An error occured while importing
\r
74 msgbox('Error! Did you type the correct name of the object?');
\r
76 elseif ~isa(y,'double')
\r
77 msgbox('Error! The object must be a matrix consisting of doubles!');
\r
80 % Object imported successfully
\r
81 str=['[',num2str(size(y,1)),' x ',num2str(size(y,2))];
\r
83 str=[str, ' x ',num2str(size(y,3))];
\r
89 set(handles.sim_y_size,'String',str);
\r
90 guidata(hObject,handles);
\r
93 if strcmp(action,'x_import')
\r
94 % Make this a shared function between x and y instead!
\r
95 x_edit_str=get(handles.sim_x_edit,'String');
\r
96 if isempty(x_edit_str)
\r
100 x=evalin('base',x_edit_str,'[]');
\r
102 % An error occured while importing
\r
103 msgbox('Error! Did you type the correct name of the object?');
\r
105 elseif ~isa(x,'double')
\r
106 msgbox('Error! The object must be a matrix consisting of doubles!');
\r
109 % Object imported successfully
\r
110 str=['[',num2str(size(x,1)),' x ',num2str(size(x,2))];
\r
112 str=[str, ' x ',num2str(size(x,3))];
\r
118 set(handles.sim_x_size,'String',str);
\r
119 guidata(hObject,handles);
\r
122 if strcmp(action,'filter')
\r
124 model_objects=handles.model_objects;
\r
126 simulator_mode=handles.simulator_mode;
\r
128 % A simulator is chosen as input data
\r
129 sim_model_value=get(handles.sim_model,'value')-1;
\r
130 if sim_model_value<1
\r
131 msgbox('You must select a model for the simulation!');
\r
135 % A data matrix is chosen as input data
\r
136 if isempty(handles.y)
\r
137 msgbox('No y(t) data is available!');
\r
142 filt_model_value=get(handles.filt_model,'value')-1;
\r
143 if filt_model_value<1
\r
144 msgbox('You must select a model for the filter!');
\r
148 xhat_mode=get(handles.xhat_check,'value');
\r
149 xpred_mode=get(handles.xpred_check,'value');
\r
150 if xhat_mode+xpred_mode==0
\r
151 msgbox('At least one of ''xhat'' or ''xpred'' must be checked');
\r
155 % OK, we're ready to filter!
\r
159 filtobj=handles.filtobj;
\r
160 filt_model=model_objects{filt_model_value};
\r
162 % Set the u(t) varsize, if it exists. If not - do nothing!
\r
163 data={'f','gw','gu','h','hu'};
\r
164 for i=1:length(data)
\r
165 eval('varsize=get(filt_model.(data{i}),''varsize'');','varsize=[];'); % Try / Catch
\r
166 if ~isempty(varsize);
\r
167 varsize(3)=size(handles.u,1);
\r
168 filt_model.(data{i})=set(filt_model.(data{i}),'varsize',varsize);
\r
172 set(filtobj,'model',filt_model);
\r
174 states=length(get(filt_model,'x0'));
\r
178 model_objects=handles.model_objects;
\r
179 sim_model=model_objects{sim_model_value};
\r
180 simobj=simulator(sim_model);
\r
181 steps=handles.simulation_steps;
\r
182 xtrue=zeros(states,steps,runs);
\r
184 xtrue=handles.xtrue;
\r
185 steps=size(handles.y,2);
\r
186 if size(handles.y,2)~=size(xtrue,2)&&~isempty(xtrue)
\r
187 msgbox('The number of colums of the y(t) and the true x(t) matrices must be identical!');
\r
193 xhat=zeros(states,steps,runs);
\r
199 xpred=zeros(states,steps,runs);
\r
204 wbar=waitbar(0,'Filtering...');
\r
207 simobj=reset(simobj,t);
\r
208 simulate(simobj,steps,u);
\r
210 xtrue(:,:,i)=get(simobj,'x');
\r
212 y=handles.y(:,:,rem(i-1,size(handles.y,3))+1);
\r
214 % Maybe we should change the output parameters of filter.m!
\r
215 filtobj=reset(filtobj,t);
\r
216 filter(filtobj,y,u,t);
\r
218 xhat(:,:,i)=get(filtobj,'xhat');
\r
221 xpred(:,:,i)=get(filtobj,'xpred');
\r
223 waitbar(i/runs,wbar);
\r
227 handles.xpred=xpred;
\r
228 handles.xtrue=xtrue;
\r
229 handles.steps=steps;
\r
230 handles.Ts=get(filtobj,'Ts');
\r
231 set(filtobj,'xhat',[]); % Free some data
\r
232 set(filtobj,'xpred',[]); % Free some data
\r
233 handles.filtobj=filtobj;
\r
235 guidata(hObject,handles);
\r
239 if strcmp(action,'graph_button')
\r
240 pfgui_graph('init',hObject);
\r
243 if strcmp(action,'rmse_button')
\r
244 pfgui_rmse('init',hObject);
\r
247 if strcmp(action,'export_button')
\r
248 pfgui_export('init',hObject);
\r
251 if strcmp(action,'t_edit')
\r
252 t_edit_str=get(handles.t_edit,'String');
\r
253 if isempty(t_edit_str)
\r
254 msgbox('Please enter a value, this field can not be blank!');
\r
257 t_edit=str2num(t_edit_str);
\r
259 msgbox('The time must not be negative!');
\r
263 set(handles.t_edit,'String',num2str(t_edit)); % Format data
\r
264 guidata(hObject,handles);
\r
267 if strcmp(action,'u_edit')
\r
268 u_edit_str=get(handles.u_edit,'String');
\r
269 if isempty(u_edit_str)
\r
273 u=evalin('base',u_edit_str,'[]');
\r
275 % An error occured while importing
\r
276 msgbox('Error! Did you type the correct name of the object?');
\r
278 elseif ~isa(u,'double')
\r
279 msgbox('Error! The object must be a matrix consisting of doubles!');
\r
282 % Object imported successfully
\r
283 str=['[',num2str(size(u,1)),' x ',num2str(size(u,2)),']'];
\r
287 set(handles.u_text,'String',str);
\r
288 guidata(hObject,handles);
\r
294 if strcmp(action,'filt_edit_button')
\r
295 filter_type_value=get(handles.filt_type_select,'value');
\r
296 filter_type=class(handles.filter_objects{filter_type_value});
\r
297 funcstr=[filter_type, '_edit'];
\r
298 feval(funcstr, 'init', hObject, handles.filtobj, 'filtobj', ['pfgui_action(''filter_refresh'', ', num2str(hObject), ', ''filtobj'',',num2str(filter_type_value),');']);
\r
301 if strcmp(action,'filter_refresh')
\r
302 % Save the modified filter!
\r
303 filter_index=varargin{2};
\r
304 handles.filter_objects{filter_index}=handles.filtobj;
\r
305 guidata(hObject,handles);
\r
308 if strcmp(action,'filt_type_select')
\r
310 filter_objects=handles.filter_objects;
\r
311 handles.filtobj=filter_objects{get(handles.filt_type_select,'value')};
\r
312 guidata(hObject,handles);
\r
314 % danl_menu_select(hObject);
\r
317 if strcmp(action,'edit_runs')
\r
318 runs_str=get(handles.filt_runs_edit,'String');
\r
319 if isempty(runs_str)
\r
320 msgbox('Error! Blank field.');
\r
324 runs=str2num(runs_str);
\r
327 msgbox('Error! Invalid value.');
\r
331 set(handles.filt_runs_edit,'String',num2str(runs)); % Format data
\r
334 guidata(hObject,handles);
\r
337 if strcmp(action,'simulation_steps')
\r
338 % MAKE A FUNCTION OF THIS ONE!!!
\r
339 steps_str=get(handles.sim_steps,'String');
\r
340 if isempty(steps_str)
\r
341 msgbox('Error! Blank field.');
\r
345 steps=str2num(steps_str);
\r
348 msgbox('Error! Invalid value.');
\r
352 set(handles.sim_steps,'String',num2str(steps)); % Format data
\r
354 handles.simulation_steps=steps;
\r
355 guidata(hObject,handles);
\r
361 if strcmp(action,'rmse_runs')
\r
362 rmse_runs_str=get(handles.rmse_runs_edit,'String');
\r
363 if isempty(rmse_runs_str)
\r
364 msgbox('Error! Blank field.');
\r
368 rmse_runs=str2num(rmse_runs_str);
\r
371 msgbox('Error! Invalid value.');
\r
375 set(handles.rmse_runs_edit,'String',num2str(rmse_runs)); % Format data
\r
377 handles.rmse_runs=rmse_runs;
\r
378 guidata(hObject,handles);
\r
381 if strcmp(action,'diskop_load')
\r
382 filename=get(handles.diskop_edit,'String');
\r
383 if isempty(filename)
\r
384 msgbox('Error! No filename is specified.');
\r
388 load(filename,'handles');
\r
389 guidata(hObject,handles);
\r
390 pfgui_refresh(hObject);
\r
391 msgbox('Guidata loaded');
\r
395 if strcmp(action,'diskop_save')
\r
396 filename=get(handles.diskop_edit,'String');
\r
397 if isempty(filename)
\r
398 msgbox('Error! No filename is specified.');
\r
402 save(filename,'handles');
\r
404 msgbox('Guidata saved successfully');
\r
408 if strcmp(action,'graph_states')
\r
409 graph_states_str=get(handles.graph_states_edit,'String');
\r
410 if isempty(graph_states_str)
\r
411 msgbox('Error! Blank field.');
\r
415 graph_states=str2num(graph_states_str);
\r
418 msgbox('Error! Invalid value.');
\r
422 set(handles.graph_states_edit,'String',num2str(graph_states)); % Format data
\r
424 handles.graph_states=graph_states;
\r
425 guidata(hObject,handles);
\r
429 if strcmp(action,'animate_axes')
\r
430 animate_axes_str=get(handles.animate_axes_edit,'String');
\r
431 if isempty(animate_axes_str)
\r
432 msgbox('Error! Blank field.');
\r
436 animate_axes=str2num(animate_axes_str);
\r
438 set(handles.animate_axes_edit,'String',num2str2(animate_axes)); % Format data
\r
440 handles.animate_axes=animate_axes;
\r
441 guidata(hObject,handles);
\r
442 pfgui_animate_text(hObject);
\r
445 if strcmp(action,'animate_states')
\r
446 animate_states_str=get(handles.animate_states_edit,'String');
\r
447 if isempty(animate_states_str)
\r
448 msgbox('Error! Blank field.');
\r
452 animate_states=str2num(animate_states_str);
\r
454 set(handles.animate_states_edit,'String',num2str2(animate_states)); % Format data
\r
456 handles.animate_states=animate_states;
\r
457 guidata(hObject,handles);
\r
458 pfgui_animate_text(hObject);
\r
461 if strcmp(action,'models_button')
\r
462 pfgui_models('init',hObject,['pfgui_action(''models_refresh'', ', num2str(hObject), ');']);
\r
465 if strcmp(action,'models_refresh')
\r