Initial commit
[pftoolbox.git] / GUI / pfgui_graph.m
blobec9903c029be5c2ead1bfdda55769f1423c62542
1 function pfgui_graph(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         xhat_length=length(parenthandles.xhat);\r
32         xpred_length=length(parenthandles.xpred);\r
34 %       xvars=parenthandles.filtobj.model.xvars;\r
35         xvars=get(get(parenthandles.filtobj,'model'),'xvars');\r
37         if xhat_length&&xpred_length\r
38                 lnames=cell(1,2*length(xvars));\r
39                 for i=1:length(xvars);\r
40                         lnames{i}=[xvars{i},' [xhat]'];\r
41                 end;\r
42                 for i=1:length(xvars);\r
43                         lnames{i+length(xvars)}=[xvars{i},' [xpred]'];\r
44                 end;\r
45                 lindex=[1:length(xvars), 1:length(xvars)];\r
46                 larray=[repmat({'xhat'},1,length(xvars)), repmat({'xpred'},1,length(xvars))];\r
47                 lsimnames=[xvars xvars];\r
48         elseif xhat_length\r
49                 lnames=cell(1,length(xvars));\r
50                 lnames=xvars;\r
51                 lindex=[1:length(xvars)];\r
52                 larray=repmat({'xhat'},1,length(xvars));\r
53                 lsimnames=xvars;\r
54         else\r
55                 lnames=cell(1,length(xvars));\r
56                 lnames=xvars;\r
57                 lindex=[1:length(xvars)];\r
58                 larray=repmat({'xpred'},1,length(xvars));\r
59                 lsimnames=xvars;\r
60         end\r
62         hf=figure('Name','Graph command window');\r
63         uicontrol(hf,'Style','Text','String','Select the state(s) to plot:','Units','normalized','Position',[0.05 0.91 0.9 0.05],'HorizontalAlign','Left','Min',1,'Max',3,'FontName',myfont);\r
65         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,'FontName','FixedWidth');\r
67         uicontrol(hf,'Style','Radiobutton','String','Plot a specific run:','Units','normalized','Position',[0.05 0.3 0.35 0.05],'Tag','runs_check_1','callback',sprintf('pfgui_graph(''runs_check_1'',%g,%g)',hObject,hf),'Value',1,'HorizontalAlign','Left','FontName',myfont);\r
68         uicontrol(hf,'Style','Radiobutton','String','Plot the average','Units','normalized','Position',[0.05 0.22 0.35 0.05],'Tag','runs_check_2','callback',sprintf('pfgui_graph(''runs_check_2'',%g,%g)',hObject,hf),'Value',0,'HorizontalAlign','Left','FontName',myfont);\r
70         uicontrol(hf,'Style','Edit','String','1','Units','normalized','Position',[0.5 0.3 0.2 0.05],'Tag','runs_edit','callback',sprintf('pfgui_graph(''run_edit'',%g,%g)',hObject,hf),'HorizontalAlign','Left','FontName',myfont,'Enable','on');\r
71         uicontrol(hf,'Style','Text','String',sprintf('Max: %d',max(size(parenthandles.xhat,3),size(parenthandles.xpred,3))),'Units','normalized','Position',[0.75 0.3 0.2 0.05],'HorizontalAlign','Left','FontName',myfont);\r
74         uicontrol(hf,'Style','Pushbutton','String','PLOT!','Units','normalized','Position',[0.05 0.1 0.26 0.05],'callback',sprintf('pfgui_graph(''graph'',%g,%g)',hObject,hf),'HorizontalAlign','Left','FontName',myfont);\r
80         handles=guihandles(hf);\r
81         handles.lnames=lnames;\r
82         handles.lindex=lindex;\r
83         handles.larray=larray;\r
84         handles.lsimnames=lsimnames;\r
85         handles.run=1;\r
86         handles.average_mode=0;\r
87         guidata(hf,handles);\r
89 elseif strcmp(action,'runs_check_1');\r
90         hf=varargin{1};\r
91         handles=guidata(hf);\r
93         set(handles.runs_check_1,'Value',1);\r
94         set(handles.runs_check_2,'Value',0);\r
95         set(handles.runs_edit,'Enable','on');\r
97         handles.average_mode=0;\r
98         guidata(hf,handles);\r
100 elseif strcmp(action,'runs_check_2');\r
101         hf=varargin{1};\r
102         handles=guidata(hf);\r
104         set(handles.runs_check_2,'Value',1);\r
105         set(handles.runs_check_1,'Value',0);\r
106         set(handles.runs_edit,'Enable','off');\r
108         handles.average_mode=1;\r
109         guidata(hf,handles);\r
111 elseif strcmp(action,'run_edit');\r
112         hf=varargin{1};\r
113         handles=guidata(hf);\r
114         parenthandles=guidata(hObject);\r
116         runs_str=get(handles.runs_edit,'String');\r
117         if isempty(runs_str)\r
118                 msgbox('Error! Blank field.');\r
119                 return;\r
120         end;\r
122         runs=str2num(runs_str);\r
124         if runs<1||runs>max(size(parenthandles.xhat,3),size(parenthandles.xpred,3))\r
125                 msgbox('Error! Invalid value.');\r
126                 return;\r
127         end;\r
129         set(handles.runs_edit,'String',num2str(runs));  % Format data\r
131         handles.run=runs;\r
132         guidata(hf,handles);\r
134 elseif strcmp(action,'graph');\r
135         hf=varargin{1};\r
136         parenthandles=guidata(hObject);\r
137         handles=guidata(hf);\r
138         \r
139         states=get(handles.state_select,'value');\r
140         if isempty(states)\r
141                 msgbox('Please select at least one state');\r
142                 return;\r
143         end;\r
145         Ts=parenthandles.filtobj.Ts;\r
147         figure('Name','Graph data');\r
150         plotstr='plot(';\r
151         simnames={};\r
153         if handles.average_mode\r
154                 xhat_avg=mean(parenthandles.xhat,3);\r
155                 xpred_avg=mean(parenthandles.xpred,3);\r
156                 xtrue_avg=mean(parenthandles.xtrue,3);\r
157                         \r
158                 if ~isempty(parenthandles.xtrue)\r
159                         simnames={};\r
160                         simmedindices=[];\r
161                         for i=1:length(states)\r
162                                 if isempty(find(simmedindices==handles.lindex(states(i))));\r
163                                         % Not yet plotted\r
164                                         simnames{end+1}=[handles.lsimnames{states(i)},' [true]'];\r
165                                         plotstr=[plotstr,sprintf('Ts,xtrue_avg(%d,:),',handles.lindex(states(i)))];\r
166                                         simmedindices(end+1)=handles.lindex(states(i));\r
167                                 end\r
168                         end\r
169                 end\r
171                 for i=1:length(states)\r
172                         if strcmp(handles.larray{states(i)},'xhat')\r
173                                 plotstr=[plotstr,sprintf('Ts,xhat_avg(%d,:),',handles.lindex(states(i)))];\r
174                         else\r
175                                 plotstr=[plotstr,sprintf('Ts,xpred_avg(%d,:),',handles.lindex(states(i)))];\r
176                         end\r
177                 end\r
178         else\r
179                 if ~isempty(parenthandles.xtrue)\r
180                         simnames={};\r
181                         simmedindices=[];\r
182                         for i=1:length(states)\r
183                                 if isempty(find(simmedindices==handles.lindex(states(i))));\r
184                                         % Not yet plotted\r
185                                         simnames{end+1}=[handles.lsimnames{states(i)},' [true]'];\r
186                                         plotstr=[plotstr,sprintf('Ts,parenthandles.xtrue(%d,:,%d),',handles.lindex(states(i)),rem(handles.run-1,size(parenthandles.xtrue,3))+1)];\r
187                                         simmedindices(end+1)=handles.lindex(states(i));\r
188                                 end\r
189                         end\r
190                 end\r
192                 for i=1:length(states)\r
193                         if strcmp(handles.larray{states(i)},'xhat')\r
194                                 plotstr=[plotstr,sprintf('Ts,parenthandles.xhat(%d,:,%d),',handles.lindex(states(i)),handles.run)];\r
195                         else\r
196                                 plotstr=[plotstr,sprintf('Ts,parenthandles.xpred(%d,:,%d),',handles.lindex(states(i)),handles.run)];\r
197                         end\r
198                 end\r
200         end\r
203         plotstr=[plotstr(1:end-1),')'];\r
204         eval(plotstr);          % PLOT!!!\r
206         legend(simnames{:},handles.lnames{states})\r
207         \r
208 end\r