1 | %CREATE_SHORT
|
---|
2 | %
|
---|
3 | % Create short Contents.m in distools directory
|
---|
4 |
|
---|
5 | function create_short
|
---|
6 |
|
---|
7 | disdir = which('distools_long.m');
|
---|
8 | disdir = fileparts(disdir);
|
---|
9 | r = readf(fullfile(disdir,'distools_long.m'));
|
---|
10 | k = grep(r,'%%');
|
---|
11 | s = listn(r,k);
|
---|
12 | s = strrep(s,'%%','% ');
|
---|
13 | writf(fullfile(disdir,'Contents.m'),s);
|
---|
14 |
|
---|
15 | return
|
---|
16 |
|
---|
17 | %WRITF Write file
|
---|
18 | %
|
---|
19 | % writf(file,r)
|
---|
20 | % Write file from string r
|
---|
21 |
|
---|
22 | function writf(file,r)
|
---|
23 | fid = fopen(file,'w');
|
---|
24 | if fid < 0
|
---|
25 | error('Cannot open file')
|
---|
26 | end
|
---|
27 | fprintf(fid,'%c',r);
|
---|
28 | fclose(fid);
|
---|
29 | return
|
---|
30 |
|
---|
31 | %READF Readfile
|
---|
32 | %
|
---|
33 | % [r,n] = readf(file,newline)
|
---|
34 | % Reads file into string r. The number of lines
|
---|
35 | % is returned in n.
|
---|
36 |
|
---|
37 | function [r,n] = readf(file,newline)
|
---|
38 | if nargin < 2, newline = 13; end
|
---|
39 | fid = fopen(deblank(file),'r');
|
---|
40 | if fid < 0
|
---|
41 | error(['Cann''t open ' file])
|
---|
42 | end
|
---|
43 | r = fscanf(fid,'%c');
|
---|
44 | fclose(fid);
|
---|
45 | n = length(find(r==newline));
|
---|
46 | if r(length(r)) ~= newline, n = n + 1; end
|
---|
47 | return
|
---|
48 |
|
---|
49 | %LISTN List lines specified by their line number
|
---|
50 | %
|
---|
51 | % t = listn(r,n)
|
---|
52 | % Get the lines in r given by the line numbers in n.
|
---|
53 | function t = listn(r,n)
|
---|
54 | k = [0,find(r==newline)];
|
---|
55 | t = [];
|
---|
56 | for j = n
|
---|
57 | t = [t,r(k(j)+1:k(j+1))];
|
---|
58 | end
|
---|
59 | return
|
---|
60 |
|
---|
61 | %GREP Get line specific lines
|
---|
62 | %
|
---|
63 | % [k,n] = grep(r,s)
|
---|
64 | % Get the numbers of all lines in the set of lines r
|
---|
65 | % that contain s.
|
---|
66 | % n is the total number of lines.
|
---|
67 |
|
---|
68 | function [k,z] = grep(r,s)
|
---|
69 | n = [0,find(r==newline)];
|
---|
70 | m = findstr(r,s);
|
---|
71 | [i,j] = sort([n,m]);
|
---|
72 | q = [0,j(1:length(j)-1)]-j;
|
---|
73 | k = j(find(q>0))-1;
|
---|
74 | z = length(n)-1; % # of lines
|
---|
75 | return
|
---|