1 | function SubFuns(mFile) |
---|
2 | %SUBFUNS lists all function declaration lines in specified MFILE. |
---|
3 | % SUBFUNS(MFILE) displays list to Command Window. |
---|
4 | % |
---|
5 | %Example: |
---|
6 | % SubFuns SubFuns |
---|
7 | % |
---|
8 | %Note: |
---|
9 | % This utility uses both subfunctions and a nested function, both of |
---|
10 | % which are supported. |
---|
11 | % |
---|
12 | %See also FUNCTION. |
---|
13 | |
---|
14 | % Copyright 2004 The MathWorks, Inc. |
---|
15 | % rbemis@mathworks.com |
---|
16 | |
---|
17 | error(nargchk(1,1,nargin)) |
---|
18 | error(nargoutchk(0,0,nargout)) |
---|
19 | |
---|
20 | %make sure m-file exists (on path) |
---|
21 | if ~exist(mFile) |
---|
22 | error(['Unable to find m-file ' mFile]) |
---|
23 | end |
---|
24 | |
---|
25 | %open m-file (read access) |
---|
26 | fid = fopen(which(mFile)); |
---|
27 | |
---|
28 | %find all lines starting with 'function' signature |
---|
29 | subFunList = {}; |
---|
30 | lineNum = 0; |
---|
31 | while ~feof(fid) %check one line at a time |
---|
32 | lineNum = lineNum+1; %advance line pointer |
---|
33 | try %read line |
---|
34 | txt = fgetl(fid); |
---|
35 | catch %file read error |
---|
36 | disp(['Tried reading file ' mFile]) |
---|
37 | error(sprintf('Unable to read line %d',lineNum)) |
---|
38 | end |
---|
39 | name = FunctionName(txt); |
---|
40 | if ~isempty(name) %found one, add to list |
---|
41 | subFunList(end+1,1:2) = {lineNum,name}; |
---|
42 | end |
---|
43 | end |
---|
44 | |
---|
45 | %close file when done |
---|
46 | fclose(fid); |
---|
47 | |
---|
48 | %display results |
---|
49 | if ~isempty(subFunList) |
---|
50 | %honor loose preference |
---|
51 | if strcmp('loose',get(0,'FormatSpacing')) |
---|
52 | fprintf('\n'); |
---|
53 | end |
---|
54 | |
---|
55 | %table header |
---|
56 | fprintf(' Line: Function:\n'); |
---|
57 | |
---|
58 | %row/col entries |
---|
59 | for i=1:size(subFunList,1) |
---|
60 | lineNum = subFunList{i,1}; |
---|
61 | %fprintf(' %d',lineNum); |
---|
62 | html = OpenLink(mFile,lineNum); |
---|
63 | fprintf(' %s',html); |
---|
64 | fprintf(blanks(7-floor(log10(lineNum)))); |
---|
65 | name = subFunList{i,2}; |
---|
66 | fprintf('%s\n',name); |
---|
67 | end |
---|
68 | |
---|
69 | %extra blank line after list before prompt |
---|
70 | fprintf('\n'); |
---|
71 | end %if (list empty or not) |
---|
72 | |
---|
73 | end %SubFuns (main) |
---|
74 | |
---|
75 | |
---|
76 | %sub------------------------------------------------------------------- |
---|
77 | function name = FunctionName(txt) |
---|
78 | %FUNCTIONNAME returns name of subfunction. |
---|
79 | % Returns empty if line does not contain valid function name. |
---|
80 | % |
---|
81 | %Usage: |
---|
82 | % name = FunctionName(txt) |
---|
83 | |
---|
84 | %TODO List: |
---|
85 | % 1. double check that algorithm worked correctly |
---|
86 | |
---|
87 | %Note: MATLAB 7 includes a new TODO/FIXME tool to help maintain your code. |
---|
88 | % It's one of several new Directory Reports available from the Current |
---|
89 | % Directory Browser on the MATLAB Desktop. You can also access the |
---|
90 | % TODO/FIXME Report tool from the menu bar when the Current Directory is |
---|
91 | % selected using View, Directory Reports. |
---|
92 | |
---|
93 | error(nargchk(1,1,nargin)) |
---|
94 | error(nargoutchk(1,1,nargout)) |
---|
95 | |
---|
96 | %remove leading blanks |
---|
97 | txt = NoLeadBlanks(txt); |
---|
98 | |
---|
99 | %nested---------------------------------------% |
---|
100 | function txt = NoLeadBlanks(txt) % |
---|
101 | %NOLEADBLANK removes leading spaces. % |
---|
102 | done = false; % |
---|
103 | while ~done % |
---|
104 | if isempty(txt) % |
---|
105 | return % |
---|
106 | elseif txt(1)==' ' %leading blank % |
---|
107 | txt(1) = []; %remove % |
---|
108 | else %done (nonblank first character) % |
---|
109 | done = true; % |
---|
110 | end % |
---|
111 | end %while (done or not) % |
---|
112 | end %NoLeadBlanks (nested function)-----------% |
---|
113 | |
---|
114 | %{ |
---|
115 | Notes: |
---|
116 | (1) This use of a nested function was contrived. There is no advantage |
---|
117 | over a subfunction in terms of variable scope/sharing. Its value |
---|
118 | here was just to provide a self referential example to show a |
---|
119 | nested fuction. |
---|
120 | (2) This is also an example of a block comment (new for MATLAB 7). |
---|
121 | %} |
---|
122 | |
---|
123 | if length(txt)<8 %line too short |
---|
124 | name = []; %return empty (no valid function here) |
---|
125 | return; |
---|
126 | end |
---|
127 | |
---|
128 | if strcmp('function',txt(1:8)) %function line |
---|
129 | %strip off function keyword at beginning |
---|
130 | txt(1:8) = []; |
---|
131 | |
---|
132 | %ignore outputs |
---|
133 | if strfind(txt,'=') %outputs defined |
---|
134 | loc = strfind(txt,'='); %LHS before = |
---|
135 | txt(1:loc) = []; %ignore outputs |
---|
136 | end |
---|
137 | |
---|
138 | %remove leading blanks before name |
---|
139 | txt = NoLeadBlanks(txt); |
---|
140 | |
---|
141 | %ignore EOL comments |
---|
142 | if strfind(txt,'%') |
---|
143 | loc = strfind(txt,'%'); %comment from % on |
---|
144 | txt(loc:end) = []; %ignore inputs |
---|
145 | end |
---|
146 | |
---|
147 | %ignore inputs |
---|
148 | if strfind(txt,'(') |
---|
149 | loc = strfind(txt,'('); %inputs after ( |
---|
150 | txt(loc:end) = []; %ignore inputs |
---|
151 | end |
---|
152 | |
---|
153 | %remove trailing blanks after function name |
---|
154 | while txt(end)==' ' |
---|
155 | txt(end) = []; |
---|
156 | end |
---|
157 | |
---|
158 | %for legal function names, only thing left must be function name |
---|
159 | name = txt; |
---|
160 | else %not function line |
---|
161 | name = []; %return empty |
---|
162 | end |
---|
163 | |
---|
164 | end %FunctionName (subfunction) |
---|
165 | |
---|
166 | |
---|
167 | %sub------------------------------------------------------------------- |
---|
168 | function html = OpenLink(mFile,lineNum) |
---|
169 | %OPENLINK generates hyperlink to open MFILE to LINENUM. |
---|
170 | % HTML = OPENLINK(MFILE,LINENUM) returns HTML code to generate hyperlink. |
---|
171 | % Displays line number. Link invokes command to open specified MFILE to |
---|
172 | % LINENUM. |
---|
173 | % |
---|
174 | %Example: |
---|
175 | % html = OpenLink(mFile,lineNum); |
---|
176 | |
---|
177 | error(nargchk(2,2,nargin)) |
---|
178 | error(nargoutchk(1,1,nargout)) |
---|
179 | |
---|
180 | %make m-file location explicit |
---|
181 | mFile = which(mFile); |
---|
182 | |
---|
183 | %Java function |
---|
184 | javaFcn = 'com.mathworks.mlservices.MLEditorServices.openDocumentToLine'; |
---|
185 | |
---|
186 | %calling syntax |
---|
187 | cmd = sprintf('matlab: %s(''%s'',%d,1,1)',javaFcn,mFile,lineNum); |
---|
188 | |
---|
189 | %hyperlink HTML script |
---|
190 | html = sprintf('<a href="%s">%d</a>',cmd,lineNum); |
---|
191 | |
---|
192 | end %OpenLink (subfunction) |
---|