1 | function varargout = toggle(action, varargin) |
---|
2 | |
---|
3 | switch action |
---|
4 | |
---|
5 | %toggle(''GenFig'',names,istatus,iavail,ifit,title,templisttag]; |
---|
6 | %============================================================= |
---|
7 | case 'GenFig' |
---|
8 | %============================================================= |
---|
9 | % Generates the figure which holds |
---|
10 | % Radio buttons for element selection by the user. |
---|
11 | % Only re-draw figure after final OK (delParFig) |
---|
12 | name=varargin(1); name=char(name{1}); |
---|
13 | status=varargin(2); status=status{1}; |
---|
14 | avail=varargin(3); avail=avail{1}; |
---|
15 | isel=varargin(4); isel=isel{1}; |
---|
16 | title=varargin(5); title=title{1}; |
---|
17 | tag=varargin(6); tag=tag{1}; |
---|
18 | tlist=varargin(1:4); %temporary lists |
---|
19 | |
---|
20 | %test if toggle panel already exists |
---|
21 | h= findobj(0,'tag',['toggle_' tag]); |
---|
22 | if ~isempty(h) |
---|
23 | delete(h); |
---|
24 | end |
---|
25 | |
---|
26 | |
---|
27 | |
---|
28 | newFig = figure('Position',[100 400 800 350],... |
---|
29 | 'UserData',tlist,... |
---|
30 | 'NumberTitle','off',... |
---|
31 | 'tag',['toggle_' tag],... |
---|
32 | 'Name',['Element Selection: ' title],... |
---|
33 | 'MenuBar','none'); |
---|
34 | |
---|
35 | xpos = 20; %initial x and y offsets of the button |
---|
36 | ypos = 330; |
---|
37 | |
---|
38 | for ind = 1:length(name) |
---|
39 | |
---|
40 | ypos = ypos - 20; |
---|
41 | if ypos < 50 %go to new column if full |
---|
42 | ypos = 310; |
---|
43 | xpos = xpos + 120; |
---|
44 | end |
---|
45 | |
---|
46 | %default no status |
---|
47 | val = 0; %bad status or not available ==> red |
---|
48 | color = 'r'; |
---|
49 | uitype='text'; |
---|
50 | uicall=' '; |
---|
51 | |
---|
52 | if ~isempty(avail) |
---|
53 | if ~isempty(find(avail==ind)) |
---|
54 | val = 0; |
---|
55 | color = 'y'; %is available ==> yellow |
---|
56 | uitype='radiobutton'; |
---|
57 | uicall='toggle(''ToggleElem'')'; |
---|
58 | end |
---|
59 | end |
---|
60 | |
---|
61 | if ~isempty(avail) & ~isempty(isel) |
---|
62 | if ~isempty(find(avail==ind)) & ... |
---|
63 | ~isempty(find(isel==ind)) |
---|
64 | val = 1; |
---|
65 | color = 'g'; %available and on ==> green |
---|
66 | uitype='radiobutton'; |
---|
67 | uicall='toggle(''ToggleElem'')'; |
---|
68 | end |
---|
69 | end |
---|
70 | |
---|
71 | if ~isempty(avail) & ~isempty(status) |
---|
72 | if isempty(find(status==ind)) | ... |
---|
73 | isempty(find(avail==ind)) |
---|
74 | end |
---|
75 | end |
---|
76 | |
---|
77 | uicontrol('Style','frame',... |
---|
78 | 'Position',[xpos-1 ypos-1 72 22]); |
---|
79 | uicontrol('Style',uitype, ... |
---|
80 | 'Position',[xpos ypos 70 20], ... |
---|
81 | 'BackgroundColor',color, ... |
---|
82 | 'String',name(ind,:), ... |
---|
83 | 'Value',val,... |
---|
84 | 'Callback',uicall,... |
---|
85 | 'tag',['tg' num2str(ind)]); |
---|
86 | end %end loop on elements |
---|
87 | |
---|
88 | uicontrol('Style','pushbutton',... |
---|
89 | 'Position',[10 10 200 30],... |
---|
90 | 'String','Load Selection',... |
---|
91 | 'Callback','toggle(''LoadSelection'')',... |
---|
92 | 'Tag','loadtoggle',... |
---|
93 | 'Userdata',tag); %Load New Values |
---|
94 | uicontrol('Style','pushbutton',... |
---|
95 | 'Position',[300 10 80 30],... |
---|
96 | 'String','Select All',... |
---|
97 | 'Callback','toggle(''SelectAll'')'); %Select All |
---|
98 | uicontrol('Style','pushbutton',... |
---|
99 | 'Position',[400 10 80 30],... |
---|
100 | 'String','Select None',... |
---|
101 | 'Callback','toggle(''SelectNone'')'); %Select None |
---|
102 | uicontrol('Style','pushbutton',... |
---|
103 | 'Position',[500 10 60 30],... |
---|
104 | 'String','Cancel',... |
---|
105 | 'Callback','delete(gcf)') %Cancel button |
---|
106 | |
---|
107 | %============================================================= |
---|
108 | case 'LoadSelection' % *** LoadSelection *** |
---|
109 | %============================================================= |
---|
110 | %Callback of the 'LoadSelection' button in the GenFig |
---|
111 | tlist = get(gcf,'UserData'); |
---|
112 | name=tlist{1}; |
---|
113 | status=tlist{2}; |
---|
114 | avail=tlist{3}; |
---|
115 | isel=tlist{4}; |
---|
116 | % |
---|
117 | % % for ind = 1:length(name) |
---|
118 | % % if ~isempty(find(status==ind)) |
---|
119 | % % h=findobj(gcf,'tag',['tg' num2str(ind)]); |
---|
120 | % % value(ind)=get(h,'Value'); |
---|
121 | % % end |
---|
122 | % % end |
---|
123 | % tlist=[tlist{1} tlist{2} tlist{3} isel]; |
---|
124 | % set(gcf,'UserData',tlist); |
---|
125 | h1=findobj(0,'Tag','loadtoggle'); %get handle of 'load' button |
---|
126 | tag=get(h1,'Userdata'); %get tag of calling gui |
---|
127 | h1=findobj(0,'Tag',tag); %get handle of calling gui |
---|
128 | instr=get(h1,'Userdata'); %get instruction set from calling gui |
---|
129 | eval(instr) %evaluate instruction set |
---|
130 | |
---|
131 | %============================================================= |
---|
132 | case 'ToggleElem' % *** ToggleElem *** |
---|
133 | %============================================================= |
---|
134 | % Callback for each radio button. |
---|
135 | % Toggle the radio color |
---|
136 | |
---|
137 | ind = get(gcbo,'tag'); %tag of radio button |
---|
138 | ind = str2num(ind(3:length(ind))); |
---|
139 | |
---|
140 | %plot is unaffected until the 'OK' button is pushed. |
---|
141 | tlist = get(gcf,'UserData'); |
---|
142 | tindx=tlist{4}; |
---|
143 | if get(gcbo,'Value') == 1 %radio button true |
---|
144 | set(gcbo,'BackgroundColor','g'); |
---|
145 | tindx=sort([tindx ind]); |
---|
146 | end |
---|
147 | if get(gcbo,'Value') == 0 %radio button false |
---|
148 | set(gcbo,'BackgroundColor','y'); |
---|
149 | tindx(find(tindx==ind))=0; %set element to zero |
---|
150 | tindx=tindx(find(tindx)); |
---|
151 | end |
---|
152 | tindx=tindx(find(tindx)); |
---|
153 | |
---|
154 | tlist=[{tlist{1}} {tlist{2}} {tlist{3}} {tindx}]; |
---|
155 | set(gcf,'UserData',tlist); |
---|
156 | |
---|
157 | %============================================================= |
---|
158 | case 'SelectAll' % *** SelectAll *** |
---|
159 | %============================================================= |
---|
160 | tlist = get(gcf,'UserData'); |
---|
161 | name=tlist{1}; |
---|
162 | status=tlist{2}; |
---|
163 | avail=tlist{3}; |
---|
164 | tindx=[]; |
---|
165 | |
---|
166 | for ind = 1:length(name) |
---|
167 | if ~isempty(find(avail==ind)) & ... |
---|
168 | ~isempty(find(status==ind)) %available & status ok |
---|
169 | h=findobj(gcf,'tag',['tg' num2str(ind)]); |
---|
170 | set(h,'BackGroundColor','g','Value',1); |
---|
171 | tindx(ind)=ind; |
---|
172 | end |
---|
173 | end |
---|
174 | tindx=tindx(find(tindx)); |
---|
175 | tlist=[{tlist{1}} {tlist{2}} {tlist{3}} {tindx}]; |
---|
176 | set(gcf,'UserData',tlist); |
---|
177 | |
---|
178 | %============================================================= |
---|
179 | case 'SelectNone' % *** SelectNone *** |
---|
180 | %============================================================= |
---|
181 | tlist = get(gcf,'UserData'); |
---|
182 | name=tlist{1}; |
---|
183 | status=tlist{2}; |
---|
184 | avail=tlist{3}; |
---|
185 | tindx=[]; |
---|
186 | |
---|
187 | for ind = 1:length(name) |
---|
188 | if ~isempty(find(avail==ind)) & ... |
---|
189 | ~isempty(find(status==ind)) %available & status ok |
---|
190 | h=findobj(gcf,'tag',['tg' num2str(ind)]); |
---|
191 | set(h,'BackGroundColor','y','Value',0); |
---|
192 | end |
---|
193 | end |
---|
194 | tlist=[{tlist{1}} {tlist{2}} {tlist{3}} {tindx}]; |
---|
195 | set(gcf,'UserData',tlist); |
---|
196 | |
---|
197 | %============================================================= |
---|
198 | otherwise |
---|
199 | disp('Warning: no CASE found in toggle'); |
---|
200 | disp(action); |
---|
201 | end % End Switchyard |
---|
202 | |
---|
203 | |
---|
204 | |
---|
205 | |
---|
206 | |
---|
207 | %******************** |
---|
208 | function [word,k] = getWord(line, i) |
---|
209 | %******************** |
---|
210 | |
---|
211 | %getWord reads one 'word' or token at a time from a line of char. |
---|
212 | |
---|
213 | %Input: line is the line being read, i is the pointer to the index |
---|
214 | % within that line. |
---|
215 | |
---|
216 | %Output:word is the token being returned to the main program. |
---|
217 | % k is the returned value of i for the main to keep track of. |
---|
218 | |
---|
219 | |
---|
220 | word = []; |
---|
221 | |
---|
222 | if ~isempty(line) |
---|
223 | |
---|
224 | while line(i) == ' ' |
---|
225 | i = i + 1; |
---|
226 | end |
---|
227 | |
---|
228 | while line(i) ~= ' ' |
---|
229 | word = [word line(i)]; |
---|
230 | i = i+1; |
---|
231 | |
---|
232 | if i > length(line) |
---|
233 | break; |
---|
234 | end |
---|
235 | end |
---|
236 | |
---|
237 | end |
---|
238 | |
---|
239 | k = i; |
---|
240 | |
---|
241 | |
---|
242 | |
---|
243 | %************************* |
---|
244 | function [index,num,k] = getElmnt(line,i) |
---|
245 | %************************* |
---|
246 | |
---|
247 | % getElmnt reads the values stored for a specific element in the file |
---|
248 | % for use in the array. |
---|
249 | |
---|
250 | % Input: line is the line of text from the file being read. |
---|
251 | % i is the index pointer in that line. |
---|
252 | |
---|
253 | % Output:index and num are data returned to the main program. |
---|
254 | % index is the element index (ex: ibpmx) |
---|
255 | % num is the data for that element (ex: 1.83E-01). |
---|
256 | |
---|
257 | % k is the value of i returned to the main program |
---|
258 | % to keep track of things. |
---|
259 | |
---|
260 | |
---|
261 | while line(i) == ' ' |
---|
262 | i = i + 1; |
---|
263 | end |
---|
264 | |
---|
265 | index = []; |
---|
266 | while ~(line(i) == ' ') & ~(line(i) == '-') |
---|
267 | index = [index line(i)]; |
---|
268 | i = i + 1; |
---|
269 | end |
---|
270 | index = str2num(index); |
---|
271 | |
---|
272 | if line(i) == ' ' |
---|
273 | i = i + 1; |
---|
274 | end |
---|
275 | |
---|
276 | num = []; |
---|
277 | while line(i) ~= ' ' |
---|
278 | num = [num line(i)]; |
---|
279 | i = i + 1; |
---|
280 | |
---|
281 | if i > length(line) |
---|
282 | break; |
---|
283 | end |
---|
284 | end |
---|
285 | num = sci2num(num); |
---|
286 | |
---|
287 | k = i; |
---|
288 | |
---|
289 | |
---|
290 | |
---|
291 | %********************* |
---|
292 | function num = sci2num(str) |
---|
293 | %********************* |
---|
294 | |
---|
295 | %Transforms a number from scientific notation to standard form |
---|
296 | %for the program to manipulate. |
---|
297 | |
---|
298 | a = []; |
---|
299 | |
---|
300 | i = 1; |
---|
301 | while str(i) ~= 'E' |
---|
302 | a = [a str(i)]; |
---|
303 | i = i + 1; |
---|
304 | end |
---|
305 | |
---|
306 | a = str2num(a); |
---|
307 | |
---|
308 | sign = str(i + 1); |
---|
309 | i = i + 2; |
---|
310 | |
---|
311 | p = [str(i) str(i + 1)]; |
---|
312 | p = str2num(p); |
---|
313 | |
---|
314 | if sign == '+' |
---|
315 | num = a*(10^p); |
---|
316 | else |
---|
317 | num = a*(10^(p * -1)); |
---|
318 | end |
---|