1 |
%MKCONTNT Make new contents.m file in the current working directory |
---|
2 |
% |
---|
3 |
%Copies the H1 line (first comment line) of all m-files found |
---|
4 |
%in the current working directory to a file named "contents.m". |
---|
5 |
%If such a file already exists, a backup copy of it is made to |
---|
6 |
%"contents.old". |
---|
7 |
% |
---|
8 |
%For example, if the user has written several m-files |
---|
9 |
%in the directory C:\MATLAB\MY_MFILES, he needs to make this the |
---|
10 |
%current working directory (using either CD or the path browser) |
---|
11 |
%and then type "mkcontnt" at the Matlab prompt. |
---|
12 |
% |
---|
13 |
%It is important to note that any fancy editing done to a previous |
---|
14 |
%version of contents.m will be lost. Only the top two lines from the |
---|
15 |
%old version are copied to the new version, but that number can easily |
---|
16 |
%be increased by minor modifications to the code. Use the top few |
---|
17 |
%lines of your contents.m files to describe in general terms what kinds |
---|
18 |
%of tasks are performed by your m-files. |
---|
19 |
% |
---|
20 |
%Take the habit of writing informative H1 lines!!! |
---|
21 |
|
---|
22 |
%Tested with Matlab 5.2.1 for Windows 95 |
---|
23 |
% |
---|
24 |
%Author: Denis Gilbert, Ph.D., physical oceanography |
---|
25 |
%Maurice Lamontagne Institute, Department of Fisheries and Oceans Canada |
---|
26 |
%email: gilbertd@dfo-mpo.gc.ca |
---|
27 |
%August 1998; Last revision: December 07, 1998 |
---|
28 |
|
---|
29 |
disp(['Creating contents.m in ' pwd]) |
---|
30 |
%Check if a contents.m file already exists in the current directory |
---|
31 |
if exist([pwd filesep 'contents.m'])==0 % Contents.m does not exist in pwd |
---|
32 |
line1 = '%Write text describing the m-files in this directory'; |
---|
33 |
line2 = '%Write text describing the m-files in this directory (continued)'; |
---|
34 |
else %Open current version of contents.m and save its first two lines |
---|
35 |
fid=fopen('contents.m','r'); |
---|
36 |
line1=fgetl(fid); line2=fgetl(fid); |
---|
37 |
fclose(fid); |
---|
38 |
%Make backup copy before deleting contents.m |
---|
39 |
copyfile('contents.m','contents.old'); |
---|
40 |
delete contents.m %Delete current version of contents.m |
---|
41 |
end |
---|
42 |
|
---|
43 |
files = what; % Structure with fields files.m, files.mat, etc. |
---|
44 |
%Note: the field files.m does not include contents.m (IMPORTANT) |
---|
45 |
%Do not displace this line of code above or below its present location |
---|
46 |
%to avoid error messages. |
---|
47 |
|
---|
48 |
if length(files.m)==0 |
---|
49 |
warning('No m-files found in this directory') |
---|
50 |
return |
---|
51 |
end |
---|
52 |
|
---|
53 |
blank_line = '% '; %Blank line |
---|
54 |
fcontents = fopen('contents.m','w'); %Write a new version of contents.m |
---|
55 |
fprintf(fcontents,'%s\n',line1); %Copy descriptive header text from previous version |
---|
56 |
fprintf(fcontents,'%s\n',line2); %Copy descriptive header text (continued) |
---|
57 |
fprintf(fcontents,'%s\n',blank_line);%Third line is blank |
---|
58 |
|
---|
59 |
%Make sure all file names are in lowercase to allow proper alphabetical sorting |
---|
60 |
files.m = lower(files.m); |
---|
61 |
files.m = sort(files.m); %Sort filenames in alphabetical order |
---|
62 |
|
---|
63 |
%Write H1 lines to contents.m if they exist |
---|
64 |
for i = 1:length(files.m) |
---|
65 |
fid=fopen(files.m{i},'r'); %Cell array of sorted file names |
---|
66 |
%Search for first commented line (H1 line) |
---|
67 |
count_percent = 0; |
---|
68 |
while count_percent < 1 & feof(fid)==0; |
---|
69 |
%True as long as we do not encounter a line with a "%" sign |
---|
70 |
%or reach the end of file |
---|
71 |
line = fgetl(fid); |
---|
72 |
if length(line) > 0 %Allow for possibility that some lines may be empty |
---|
73 |
if ~isempty(findstr(line,'%')) %LOOK for percent sign anywhere in the line |
---|
74 |
count_percent = count_percent + 1; |
---|
75 |
fprintf(fcontents,'%s\n',line); %Write H1 line to contents.m |
---|
76 |
end |
---|
77 |
end |
---|
78 |
if feof(fid)==1 %End of file encountered without finding a single percent sign |
---|
79 |
fprintf(fcontents,'%s\n',blank_line); %Write blank line to contents.m |
---|
80 |
end |
---|
81 |
end |
---|
82 |
fclose(fid); |
---|
83 |
end |
---|
84 |
|
---|
85 |
fclose(fcontents); |
---|