Initial commit
[pftoolbox.git] / GUI / pfgui_action.m
blobe75fcb795f746777de8d0f1ad206fc137063cbf6
1 function pfgui_action(action,hObject, 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 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
42 end\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
58         \r
59         handles.simulator_mode=false;\r
60         guidata(hObject,handles);\r
62 end\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
68                 y=[];\r
69                 str='[No data]';\r
70         else\r
71                 y=evalin('base',y_edit_str,'[]');\r
72                 if isempty(y)\r
73                         % An error occured while importing\r
74                         msgbox('Error! Did you type the correct name of the object?');\r
75                         return;\r
76                 elseif ~isa(y,'double')\r
77                         msgbox('Error! The object must be a matrix consisting of doubles!');\r
78                         return;\r
79                 else\r
80                         % Object imported successfully\r
81                         str=['[',num2str(size(y,1)),' x ',num2str(size(y,2))];\r
82                         if size(y,3)>1\r
83                                 str=[str, ' x ',num2str(size(y,3))];\r
84                         end                     \r
85                         str=[str,']'];\r
86                 end\r
87         end;\r
88         handles.y=y;\r
89         set(handles.sim_y_size,'String',str);\r
90         guidata(hObject,handles);\r
91 end\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
97                 x=[];\r
98                 str='[No data]';\r
99         else\r
100                 x=evalin('base',x_edit_str,'[]');\r
101                 if isempty(x)\r
102                         % An error occured while importing\r
103                         msgbox('Error! Did you type the correct name of the object?');\r
104                         return;\r
105                 elseif ~isa(x,'double')\r
106                         msgbox('Error! The object must be a matrix consisting of doubles!');\r
107                         return;\r
108                 else\r
109                         % Object imported successfully\r
110                         str=['[',num2str(size(x,1)),' x ',num2str(size(x,2))];\r
111                         if size(x,3)>1\r
112                                 str=[str, ' x ',num2str(size(x,3))];\r
113                         end                     \r
114                         str=[str,']'];\r
115                 end\r
116         end;\r
117         handles.xtrue=x;\r
118         set(handles.sim_x_size,'String',str);\r
119         guidata(hObject,handles);\r
120 end\r
122 if strcmp(action,'filter')\r
124         model_objects=handles.model_objects;\r
126         simulator_mode=handles.simulator_mode;\r
127         if 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
132                         return;\r
133                 end\r
134         else\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
138                         return;\r
139                 end\r
140         end\r
141         \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
145                 return;\r
146         end\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
152                 return;\r
153         end\r
155         % OK, we're ready to filter!\r
156         t=handles.t;\r
157         u=handles.u;\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
169                 end\r
170         end\r
172         set(filtobj,'model',filt_model);\r
174         states=length(get(filt_model,'x0'));\r
175         runs=handles.runs;\r
177         if simulator_mode\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
183         else\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
188                         return;\r
189                 end\r
190         end;\r
192         if xhat_mode\r
193                 xhat=zeros(states,steps,runs);\r
194         else\r
195                 xhat=[];\r
196         end;\r
198         if xpred_mode\r
199                 xpred=zeros(states,steps,runs);\r
200         else\r
201                 xpred=[];\r
202         end\r
204         wbar=waitbar(0,'Filtering...');\r
205         for i=1:runs\r
206                 if simulator_mode\r
207                         simobj=reset(simobj,t);\r
208                         simulate(simobj,steps,u);\r
209                         y=get(simobj,'y');\r
210                         xtrue(:,:,i)=get(simobj,'x');\r
211                 else\r
212                         y=handles.y(:,:,rem(i-1,size(handles.y,3))+1);          \r
213                 end\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
217                 if xhat_mode\r
218                         xhat(:,:,i)=get(filtobj,'xhat');\r
219                 end\r
220                 if xpred_mode\r
221                         xpred(:,:,i)=get(filtobj,'xpred');\r
222                 end\r
223                 waitbar(i/runs,wbar);\r
224         end\r
226         handles.xhat=xhat;\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
234         \r
235         guidata(hObject,handles);\r
236         close(wbar);\r
237 end\r
239 if strcmp(action,'graph_button')\r
240         pfgui_graph('init',hObject);\r
241 end\r
243 if strcmp(action,'rmse_button')\r
244         pfgui_rmse('init',hObject);\r
245 end\r
247 if strcmp(action,'export_button')\r
248         pfgui_export('init',hObject);\r
249 end\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
255                 return;\r
256         end\r
257         t_edit=str2num(t_edit_str);\r
258         if t_edit<0\r
259                 msgbox('The time must not be negative!');\r
260                 return;\r
261         end;\r
262         handles.t=t_edit;\r
263         set(handles.t_edit,'String',num2str(t_edit));   % Format data\r
264         guidata(hObject,handles);\r
265 end\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
270                 u=[];\r
271                 str='[No u(t)]';\r
272         else\r
273                 u=evalin('base',u_edit_str,'[]');\r
274                 if isempty(u)\r
275                         % An error occured while importing\r
276                         msgbox('Error! Did you type the correct name of the object?');\r
277                         return;\r
278                 elseif ~isa(u,'double')\r
279                         msgbox('Error! The object must be a matrix consisting of doubles!');\r
280                         return;\r
281                 else\r
282                         % Object imported successfully\r
283                         str=['[',num2str(size(u,1)),' x ',num2str(size(u,2)),']'];\r
284                 end\r
285         end;\r
286         handles.u=u;\r
287         set(handles.u_text,'String',str);\r
288         guidata(hObject,handles);\r
290 end\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
299 end\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
306 end\r
308 if strcmp(action,'filt_type_select')\r
309         \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
315 end\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
321                 return;\r
322         end;\r
324         runs=str2num(runs_str);\r
326         if runs<1\r
327                 msgbox('Error! Invalid value.');\r
328                 return;\r
329         end;\r
331         set(handles.filt_runs_edit,'String',num2str(runs));     % Format data\r
333         handles.runs=runs;\r
334         guidata(hObject,handles);\r
335 end\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
342                 return;\r
343         end;\r
345         steps=str2num(steps_str);\r
347         if steps<1\r
348                 msgbox('Error! Invalid value.');\r
349                 return;\r
350         end;\r
352         set(handles.sim_steps,'String',num2str(steps)); % Format data\r
354         handles.simulation_steps=steps;\r
355         guidata(hObject,handles);\r
356 end\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
365                 return;\r
366         end;\r
368         rmse_runs=str2num(rmse_runs_str);\r
370         if rmse_runs<1\r
371                 msgbox('Error! Invalid value.');\r
372                 return;\r
373         end;\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
379 end\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
385                 return;\r
386         end;\r
388         load(filename,'handles');\r
389         guidata(hObject,handles);\r
390         pfgui_refresh(hObject);\r
391         msgbox('Guidata loaded');\r
392 end\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
399                 return;\r
400         end;\r
402         save(filename,'handles');\r
404         msgbox('Guidata saved successfully');\r
405 end\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
412                 return;\r
413         end;\r
415         graph_states=str2num(graph_states_str);\r
417         if graph_states<1\r
418                 msgbox('Error! Invalid value.');\r
419                 return;\r
420         end;\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
426 end\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
433                 return;\r
434         end;\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
443 end\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
449                 return;\r
450         end;\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
459 end\r
461 if strcmp(action,'models_button')\r
462         pfgui_models('init',hObject,['pfgui_action(''models_refresh'', ', num2str(hObject), ');']);\r
463 end\r
465 if strcmp(action,'models_refresh')\r
466         msgbox('FRESH!');\r
467 end\r