1 | <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" |
---|
2 | "http://www.w3.org/TR/REC-html40/loose.dtd"> |
---|
3 | <html> |
---|
4 | <head> |
---|
5 | <title>Description of subfuns</title> |
---|
6 | <meta name="keywords" content="subfuns"> |
---|
7 | <meta name="description" content="SUBFUNS lists all function declaration lines in specified MFILE."> |
---|
8 | <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> |
---|
9 | <meta name="generator" content="m2html © 2003 Guillaume Flandin"> |
---|
10 | <meta name="robots" content="index, follow"> |
---|
11 | <link type="text/css" rel="stylesheet" href="../../m2html.css"> |
---|
12 | </head> |
---|
13 | <body> |
---|
14 | <a name="_top"></a> |
---|
15 | <div><a href="../../index.html">Home</a> > <a href="#">applications</a> > <a href="index.html">common</a> > subfuns.m</div> |
---|
16 | |
---|
17 | <!--<table width="100%"><tr><td align="left"><a href="../../index.html"><img alt="<" border="0" src="../../left.png"> Master index</a></td> |
---|
18 | <td align="right"><a href="index.html">Index for applications/common <img alt=">" border="0" src="../../right.png"></a></td></tr></table>--> |
---|
19 | |
---|
20 | <h1>subfuns |
---|
21 | </h1> |
---|
22 | |
---|
23 | <h2><a name="_name"></a>PURPOSE <a href="#_top"><img alt="^" border="0" src="../../up.png"></a></h2> |
---|
24 | <div class="box"><strong>SUBFUNS lists all function declaration lines in specified MFILE.</strong></div> |
---|
25 | |
---|
26 | <h2><a name="_synopsis"></a>SYNOPSIS <a href="#_top"><img alt="^" border="0" src="../../up.png"></a></h2> |
---|
27 | <div class="box"><strong>function SubFuns(mFile) </strong></div> |
---|
28 | |
---|
29 | <h2><a name="_description"></a>DESCRIPTION <a href="#_top"><img alt="^" border="0" src="../../up.png"></a></h2> |
---|
30 | <div class="fragment"><pre class="comment">SUBFUNS lists all function declaration lines in specified MFILE. |
---|
31 | SUBFUNS(MFILE) displays list to Command Window. |
---|
32 | |
---|
33 | Example: |
---|
34 | SubFuns SubFuns |
---|
35 | |
---|
36 | Note: |
---|
37 | This utility uses both subfunctions and a nested function, both of |
---|
38 | which are supported. |
---|
39 | |
---|
40 | See also FUNCTION.</pre></div> |
---|
41 | |
---|
42 | <!-- crossreference --> |
---|
43 | <h2><a name="_cross"></a>CROSS-REFERENCE INFORMATION <a href="#_top"><img alt="^" border="0" src="../../up.png"></a></h2> |
---|
44 | This function calls: |
---|
45 | <ul style="list-style-image:url(../../matlabicon.gif)"> |
---|
46 | </ul> |
---|
47 | This function is called by: |
---|
48 | <ul style="list-style-image:url(../../matlabicon.gif)"> |
---|
49 | </ul> |
---|
50 | <!-- crossreference --> |
---|
51 | |
---|
52 | <h2><a name="_subfunctions"></a>SUBFUNCTIONS <a href="#_top"><img alt="^" border="0" src="../../up.png"></a></h2> |
---|
53 | <ul style="list-style-image:url(../../matlabicon.gif)"> |
---|
54 | <li><a href="#_sub1" class="code">function name = FunctionName(txt)</a></li><li><a href="#_sub2" class="code">function txt = NoLeadBlanks(txt)</a></li><li><a href="#_sub3" class="code">function html = OpenLink(mFile,lineNum)</a></li></ul> |
---|
55 | <h2><a name="_source"></a>SOURCE CODE <a href="#_top"><img alt="^" border="0" src="../../up.png"></a></h2> |
---|
56 | <div class="fragment"><pre>0001 <a name="_sub0" href="#_subfunctions" class="code">function SubFuns(mFile)</a> |
---|
57 | 0002 <span class="comment">%SUBFUNS lists all function declaration lines in specified MFILE.</span> |
---|
58 | 0003 <span class="comment">% SUBFUNS(MFILE) displays list to Command Window.</span> |
---|
59 | 0004 <span class="comment">%</span> |
---|
60 | 0005 <span class="comment">%Example:</span> |
---|
61 | 0006 <span class="comment">% SubFuns SubFuns</span> |
---|
62 | 0007 <span class="comment">%</span> |
---|
63 | 0008 <span class="comment">%Note:</span> |
---|
64 | 0009 <span class="comment">% This utility uses both subfunctions and a nested function, both of</span> |
---|
65 | 0010 <span class="comment">% which are supported.</span> |
---|
66 | 0011 <span class="comment">%</span> |
---|
67 | 0012 <span class="comment">%See also FUNCTION.</span> |
---|
68 | 0013 |
---|
69 | 0014 <span class="comment">% Copyright 2004 The MathWorks, Inc.</span> |
---|
70 | 0015 <span class="comment">% rbemis@mathworks.com</span> |
---|
71 | 0016 |
---|
72 | 0017 error(nargchk(1,1,nargin)) |
---|
73 | 0018 error(nargoutchk(0,0,nargout)) |
---|
74 | 0019 |
---|
75 | 0020 <span class="comment">%make sure m-file exists (on path)</span> |
---|
76 | 0021 <span class="keyword">if</span> ~exist(mFile) |
---|
77 | 0022 error([<span class="string">'Unable to find m-file '</span> mFile]) |
---|
78 | 0023 <span class="keyword">end</span> |
---|
79 | 0024 |
---|
80 | 0025 <span class="comment">%open m-file (read access)</span> |
---|
81 | 0026 fid = fopen(which(mFile)); |
---|
82 | 0027 |
---|
83 | 0028 <span class="comment">%find all lines starting with 'function' signature</span> |
---|
84 | 0029 subFunList = {}; |
---|
85 | 0030 lineNum = 0; |
---|
86 | 0031 <span class="keyword">while</span> ~feof(fid) <span class="comment">%check one line at a time</span> |
---|
87 | 0032 lineNum = lineNum+1; <span class="comment">%advance line pointer</span> |
---|
88 | 0033 <span class="keyword">try</span> <span class="comment">%read line</span> |
---|
89 | 0034 txt = fgetl(fid); |
---|
90 | 0035 <span class="keyword">catch</span> <span class="comment">%file read error</span> |
---|
91 | 0036 disp([<span class="string">'Tried reading file '</span> mFile]) |
---|
92 | 0037 error(sprintf(<span class="string">'Unable to read line %d'</span>,lineNum)) |
---|
93 | 0038 <span class="keyword">end</span> |
---|
94 | 0039 name = <a href="#_sub1" class="code" title="subfunction name = FunctionName(txt)">FunctionName</a>(txt); |
---|
95 | 0040 <span class="keyword">if</span> ~isempty(name) <span class="comment">%found one, add to list</span> |
---|
96 | 0041 subFunList(end+1,1:2) = {lineNum,name}; |
---|
97 | 0042 <span class="keyword">end</span> |
---|
98 | 0043 <span class="keyword">end</span> |
---|
99 | 0044 |
---|
100 | 0045 <span class="comment">%close file when done</span> |
---|
101 | 0046 fclose(fid); |
---|
102 | 0047 |
---|
103 | 0048 <span class="comment">%display results</span> |
---|
104 | 0049 <span class="keyword">if</span> ~isempty(subFunList) |
---|
105 | 0050 <span class="comment">%honor loose preference</span> |
---|
106 | 0051 <span class="keyword">if</span> strcmp(<span class="string">'loose'</span>,get(0,<span class="string">'FormatSpacing'</span>)) |
---|
107 | 0052 fprintf(<span class="string">'\n'</span>); |
---|
108 | 0053 <span class="keyword">end</span> |
---|
109 | 0054 |
---|
110 | 0055 <span class="comment">%table header</span> |
---|
111 | 0056 fprintf(<span class="string">' Line: Function:\n'</span>); |
---|
112 | 0057 |
---|
113 | 0058 <span class="comment">%row/col entries</span> |
---|
114 | 0059 <span class="keyword">for</span> i=1:size(subFunList,1) |
---|
115 | 0060 lineNum = subFunList{i,1}; |
---|
116 | 0061 <span class="comment">%fprintf(' %d',lineNum);</span> |
---|
117 | 0062 html = <a href="#_sub3" class="code" title="subfunction html = OpenLink(mFile,lineNum)">OpenLink</a>(mFile,lineNum); |
---|
118 | 0063 fprintf(<span class="string">' %s'</span>,html); |
---|
119 | 0064 fprintf(blanks(7-floor(log10(lineNum)))); |
---|
120 | 0065 name = subFunList{i,2}; |
---|
121 | 0066 fprintf(<span class="string">'%s\n'</span>,name); |
---|
122 | 0067 <span class="keyword">end</span> |
---|
123 | 0068 |
---|
124 | 0069 <span class="comment">%extra blank line after list before prompt</span> |
---|
125 | 0070 fprintf(<span class="string">'\n'</span>); |
---|
126 | 0071 <span class="keyword">end</span> <span class="comment">%if (list empty or not)</span> |
---|
127 | 0072 |
---|
128 | 0073 <span class="keyword">end</span> <span class="comment">%SubFuns (main)</span> |
---|
129 | 0074 |
---|
130 | 0075 |
---|
131 | 0076 <span class="comment">%sub-------------------------------------------------------------------</span> |
---|
132 | 0077 <a name="_sub1" href="#_subfunctions" class="code">function name = FunctionName(txt)</a> |
---|
133 | 0078 <span class="comment">%FUNCTIONNAME returns name of subfunction.</span> |
---|
134 | 0079 <span class="comment">% Returns empty if line does not contain valid function name.</span> |
---|
135 | 0080 <span class="comment">%</span> |
---|
136 | 0081 <span class="comment">%Usage:</span> |
---|
137 | 0082 <span class="comment">% name = FunctionName(txt)</span> |
---|
138 | 0083 |
---|
139 | 0084 <span class="comment">%TODO List:</span> |
---|
140 | 0085 <span class="comment">% 1. double check that algorithm worked correctly</span> |
---|
141 | 0086 |
---|
142 | 0087 <span class="comment">%Note: MATLAB 7 includes a new TODO/FIXME tool to help maintain your code.</span> |
---|
143 | 0088 <span class="comment">% It's one of several new Directory Reports available from the Current</span> |
---|
144 | 0089 <span class="comment">% Directory Browser on the MATLAB Desktop. You can also access the</span> |
---|
145 | 0090 <span class="comment">% TODO/FIXME Report tool from the menu bar when the Current Directory is</span> |
---|
146 | 0091 <span class="comment">% selected using View, Directory Reports.</span> |
---|
147 | 0092 |
---|
148 | 0093 error(nargchk(1,1,nargin)) |
---|
149 | 0094 error(nargoutchk(1,1,nargout)) |
---|
150 | 0095 |
---|
151 | 0096 <span class="comment">%remove leading blanks</span> |
---|
152 | 0097 txt = <a href="#_sub2" class="code" title="subfunction txt = NoLeadBlanks(txt) ">NoLeadBlanks</a>(txt); |
---|
153 | 0098 |
---|
154 | 0099 <span class="comment">%nested---------------------------------------%</span> |
---|
155 | 0100 <span class="keyword">function</span> txt = <a href="#_sub2" class="code" title="subfunction txt = NoLeadBlanks(txt) ">NoLeadBlanks</a>(txt) <span class="comment">%</span> |
---|
156 | 0101 <span class="comment">%NOLEADBLANK removes leading spaces. %</span> |
---|
157 | 0102 done = false; <span class="comment">%</span> |
---|
158 | 0103 <span class="keyword">while</span> ~done <span class="comment">%</span> |
---|
159 | 0104 <span class="keyword">if</span> isempty(txt) <span class="comment">%</span> |
---|
160 | 0105 <span class="keyword">return</span> <span class="comment">%</span> |
---|
161 | 0106 <span class="keyword">elseif</span> txt(1)==<span class="string">' '</span> <span class="comment">%leading blank %</span> |
---|
162 | 0107 txt(1) = []; <span class="comment">%remove %</span> |
---|
163 | 0108 <span class="keyword">else</span> <span class="comment">%done (nonblank first character) %</span> |
---|
164 | 0109 done = true; <span class="comment">%</span> |
---|
165 | 0110 <span class="keyword">end</span> <span class="comment">%</span> |
---|
166 | 0111 <span class="keyword">end</span> <span class="comment">%while (done or not) %</span> |
---|
167 | 0112 <span class="keyword">end</span> <span class="comment">%NoLeadBlanks (nested function)-----------%</span> |
---|
168 | 0113 |
---|
169 | 0114 <span class="comment">%{</span> |
---|
170 | 0115 Notes: |
---|
171 | 0116 (1) This use of a nested <span class="keyword">function</span> was contrived. There is no advantage |
---|
172 | 0117 over a subfunction in terms of variable scope/sharing. Its value |
---|
173 | 0118 here was just to provide a self referential example to show a |
---|
174 | 0119 nested fuction. |
---|
175 | 0120 (2) This is also an example of a block comment (new <span class="keyword">for</span> MATLAB 7). |
---|
176 | 0121 <span class="comment">%}</span> |
---|
177 | 0122 |
---|
178 | 0123 <span class="keyword">if</span> length(txt)<8 <span class="comment">%line too short</span> |
---|
179 | 0124 name = []; <span class="comment">%return empty (no valid function here)</span> |
---|
180 | 0125 <span class="keyword">return</span>; |
---|
181 | 0126 <span class="keyword">end</span> |
---|
182 | 0127 |
---|
183 | 0128 <span class="keyword">if</span> strcmp(<span class="string">'function'</span>,txt(1:8)) <span class="comment">%function line</span> |
---|
184 | 0129 <span class="comment">%strip off function keyword at beginning</span> |
---|
185 | 0130 txt(1:8) = []; |
---|
186 | 0131 |
---|
187 | 0132 <span class="comment">%ignore outputs</span> |
---|
188 | 0133 <span class="keyword">if</span> strfind(txt,<span class="string">'='</span>) <span class="comment">%outputs defined</span> |
---|
189 | 0134 loc = strfind(txt,<span class="string">'='</span>); <span class="comment">%LHS before =</span> |
---|
190 | 0135 txt(1:loc) = []; <span class="comment">%ignore outputs</span> |
---|
191 | 0136 <span class="keyword">end</span> |
---|
192 | 0137 |
---|
193 | 0138 <span class="comment">%remove leading blanks before name</span> |
---|
194 | 0139 txt = <a href="#_sub2" class="code" title="subfunction txt = NoLeadBlanks(txt) ">NoLeadBlanks</a>(txt); |
---|
195 | 0140 |
---|
196 | 0141 <span class="comment">%ignore EOL comments</span> |
---|
197 | 0142 <span class="keyword">if</span> strfind(txt,<span class="string">'%'</span>) |
---|
198 | 0143 loc = strfind(txt,<span class="string">'%'</span>); <span class="comment">%comment from % on</span> |
---|
199 | 0144 txt(loc:end) = []; <span class="comment">%ignore inputs</span> |
---|
200 | 0145 <span class="keyword">end</span> |
---|
201 | 0146 |
---|
202 | 0147 <span class="comment">%ignore inputs</span> |
---|
203 | 0148 <span class="keyword">if</span> strfind(txt,<span class="string">'('</span>) |
---|
204 | 0149 loc = strfind(txt,<span class="string">'('</span>); <span class="comment">%inputs after (</span> |
---|
205 | 0150 txt(loc:end) = []; <span class="comment">%ignore inputs</span> |
---|
206 | 0151 <span class="keyword">end</span> |
---|
207 | 0152 |
---|
208 | 0153 <span class="comment">%remove trailing blanks after function name</span> |
---|
209 | 0154 <span class="keyword">while</span> txt(end)==<span class="string">' '</span> |
---|
210 | 0155 txt(end) = []; |
---|
211 | 0156 <span class="keyword">end</span> |
---|
212 | 0157 |
---|
213 | 0158 <span class="comment">%for legal function names, only thing left must be function name</span> |
---|
214 | 0159 name = txt; |
---|
215 | 0160 <span class="keyword">else</span> <span class="comment">%not function line</span> |
---|
216 | 0161 name = []; <span class="comment">%return empty</span> |
---|
217 | 0162 <span class="keyword">end</span> |
---|
218 | 0163 |
---|
219 | 0164 <span class="keyword">end</span> <span class="comment">%FunctionName (subfunction)</span> |
---|
220 | 0165 |
---|
221 | 0166 |
---|
222 | 0167 <span class="comment">%sub-------------------------------------------------------------------</span> |
---|
223 | 0168 <a name="_sub2" href="#_subfunctions" class="code">function html = OpenLink(mFile,lineNum)</a> |
---|
224 | 0169 <span class="comment">%OPENLINK generates hyperlink to open MFILE to LINENUM.</span> |
---|
225 | 0170 <span class="comment">% HTML = OPENLINK(MFILE,LINENUM) returns HTML code to generate hyperlink.</span> |
---|
226 | 0171 <span class="comment">% Displays line number. Link invokes command to open specified MFILE to</span> |
---|
227 | 0172 <span class="comment">% LINENUM.</span> |
---|
228 | 0173 <span class="comment">%</span> |
---|
229 | 0174 <span class="comment">%Example:</span> |
---|
230 | 0175 <span class="comment">% html = OpenLink(mFile,lineNum);</span> |
---|
231 | 0176 |
---|
232 | 0177 error(nargchk(2,2,nargin)) |
---|
233 | 0178 error(nargoutchk(1,1,nargout)) |
---|
234 | 0179 |
---|
235 | 0180 <span class="comment">%make m-file location explicit</span> |
---|
236 | 0181 mFile = which(mFile); |
---|
237 | 0182 |
---|
238 | 0183 <span class="comment">%Java function</span> |
---|
239 | 0184 javaFcn = <span class="string">'com.mathworks.mlservices.MLEditorServices.openDocumentToLine'</span>; |
---|
240 | 0185 |
---|
241 | 0186 <span class="comment">%calling syntax</span> |
---|
242 | 0187 cmd = sprintf(<span class="string">'matlab: %s(''%s'',%d,1,1)'</span>,javaFcn,mFile,lineNum); |
---|
243 | 0188 |
---|
244 | 0189 <span class="comment">%hyperlink HTML script</span> |
---|
245 | 0190 html = sprintf(<span class="string">'<a href="%s">%d</a>'</span>,cmd,lineNum); |
---|
246 | 0191 |
---|
247 | 0192 <span class="keyword">end</span> <span class="comment">%OpenLink (subfunction)</span></pre></div> |
---|
248 | <hr><address>Generated on Mon 21-May-2007 15:32:41 by <strong><a href="http://www.artefact.tk/software/matlab/m2html/">m2html</a></strong> © 2003</address> |
---|
249 | </body> |
---|
250 | </html> |
---|