Initial commit
[pftoolbox.git] / GUI / pfgui_rmse.m
blob52b22f356a3892ab7a112ef296315f9e4b2ee324
1 function pfgui_rmse(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 myfont='default';\r
23 if strcmp(action,'init')\r
24         parenthandles=guidata(hObject);\r
26         if isempty(parenthandles.xhat)&&isempty(parenthandles.xpred)\r
27                 msgbox('No filtered data available!');\r
28                 return;\r
29         end\r
31         if isempty(parenthandles.xtrue)\r
32                 msgbox('No true x(t) data exists.');\r
33                 return;\r
34         end;\r
36         if ~(size(parenthandles.xtrue,3)==1||size(parenthandles.xtrue,3)==max(size(parenthandles.xhat,3),size(parenthandles.xpred,3)))\r
37                 msgbox('Wrong dimensions of the true x(t) data.');\r
38                 return;\r
39         end\r
41         xhat_length=length(parenthandles.xhat);\r
42         xpred_length=length(parenthandles.xpred);\r
44 %       xvars=parenthandles.filtobj.model.xvars;\r
45         xvars=get(get(parenthandles.filtobj,'model'),'xvars');\r
47         if xhat_length&&xpred_length\r
48                 lnames=cell(1,2*length(xvars));\r
49                 for i=1:length(xvars);\r
50                         lnames{i}=[xvars{i},' [xhat]'];\r
51                 end;\r
52                 for i=1:length(xvars);\r
53                         lnames{i+length(xvars)}=[xvars{i},' [xpred]'];\r
54                 end;\r
55                 lindex=[1:length(xvars), 1:length(xvars)];\r
56                 larray=[repmat({'xhat'},1,length(xvars)), repmat({'xpred'},1,length(xvars))];\r
57         elseif xhat_length\r
58                 lnames=cell(1,length(xvars));\r
59                 lnames=xvars;\r
60                 lindex=[1:length(xvars)];\r
61                 larray=repmat({'xhat'},1,length(xvars));\r
62         else\r
63                 lnames=cell(1,length(xvars));\r
64                 lnames=xvars;\r
65                 lindex=[1:length(xvars)];\r
66                 larray=repmat({'xpred'},1,length(xvars));\r
67         end\r
69         hf=figure('Name','RMSE command window');\r
70         uicontrol(hf,'Style','Text','String','Select the state(s) to be included in the calculations:','Units','normalized','Position',[0.05 0.91 0.9 0.05],'HorizontalAlign','Left','Min',1,'Max',3,'FontName',myfont);\r
72         uicontrol(hf,'Style','Listbox','String',cell2str(lnames),'Units','normalized','Position',[0.05 0.5 0.9 0.4],'Tag','state_select','HorizontalAlign','Left','Min',1,'Max',3,'Value',1:length(xvars),'FontName','FixedWidth');\r
74         uicontrol(hf,'Style','Pushbutton','String','RMSE!','Units','normalized','Position',[0.05 0.1 0.9 0.05],'callback',sprintf('pfgui_rmse(''rmse'',%g,%g)',hObject,hf),'HorizontalAlign','Left','FontName',myfont);\r
76         handles=guihandles(hf);\r
77         handles.lnames=lnames;\r
78         handles.lindex=lindex;\r
79         handles.larray=larray;\r
80         guidata(hf,handles);\r
83 elseif strcmp(action,'rmse');\r
84         hf=varargin{1};\r
85         parenthandles=guidata(hObject);\r
86         handles=guidata(hf);\r
87         \r
88         states=get(handles.state_select,'value');\r
90         if isempty(states)\r
91                 msgbox('Please select at least one state');\r
92                 return;\r
93         end;\r
95         Ts=parenthandles.filtobj.Ts;\r
98         xhat_states=[];\r
99         xpred_states=[];\r
100         for i=1:length(states);\r
101                 if strcmp(handles.larray{states(i)},'xhat')\r
102                         xhat_states(end+1)=handles.lindex(i);\r
103                 else\r
104                         xpred_states(end+1)=handles.lindex(i);\r
105                 end\r
106         end\r
107         \r
109         plotstr='plot(';\r
110         simnames={};\r
113         figure;\r
115         % Hej Gustaf. Jag antar att du vill verifiera RMSE-beräkningarna :)\r
116         % Det är inte så snyggt, men jag ville inte slösa något krut på något som kanske\r
117         % ändå ändras snart... Du får fixa och trixa på fyra ställen... Du ser dem, förstås\r
118         \r
119         legendcell={};\r
120         runs=max(size(parenthandles.xhat,3),size(parenthandles.xpred,3));\r
121         if xhat_states\r
122                 if size(parenthandles.xtrue,3)==runs\r
123                         rmse=sqrt(sum(sum((parenthandles.xhat(xhat_states,:,:)-parenthandles.xtrue(xhat_states,:,:)).^2,3),1)/runs);\r
124                 else                            \r
125                         rmse=sqrt(sum(sum((parenthandles.xhat(xhat_states,:,:)-repmat(parenthandles.xtrue(xhat_states,:,1),[1 1 runs])).^2,3),1)/runs);\r
126                 end\r
127                 plot(Ts,rmse,getcolors(1));\r
128                 legendcell{end+1}='RMSE xhat';\r
129                 hold on\r
130         else\r
131                 xhat_rmse=[];\r
132         end;\r
134         if xpred_states\r
135                 if size(parenthandles.xtrue,3)==runs\r
136                         xpred_rmse=sqrt(sum(sum((parenthandles.xpred(xpred_states,:,:)-parenthandles.xtrue(xpred_states,:,:)).^2,3),1)/runs);\r
137                 else\r
138                         xpred_rmse=sqrt(sum(sum((parenthandles.xpred(xpred_states,:,:)-repmat(parenthandles.xtrue(xpred_states,:,1),[1 1 runs])).^2,3),1)/runs);\r
139                 end\r
140                 plot(Ts,xpred_rmse,getcolors(2));\r
141                 legendcell{end+1}='RMSE xpred';\r
142         else\r
143                 xpred_rmse=[];\r
144         end;\r
146         legend(legendcell{:});\r
147         \r
148 end\r