Code covered by the BSD License  

Highlights from
extracthelp

from extracthelp by Andrey Popov
Extract the help information of all M function in a folder

extracthelp(nocopyright)
% EXTRACTHELP Extract the help lines of all files in the current directory
%
% extracthelp()  
%   Creates a subfolder 'phelp' in the current folder and for
% each .m file in the current folder creates a corresponding file in \phelp
% containing only the first commented lines (i.e., help of the function).
%
% !!!   If the \phelp folder and the .M files in it already exist     !!!
% !!!   they are overwritten                                          !!!
%
% The comment's extraction starts from the first % sign in the file and
% stops at the first non-commented line. By default the function allows for
% 1 empty line between the first and the second comment blocks, since
% normally the second comment block contains the copyright information. To
% exctract only the help shown by the help function use: 
% extracthelp('nocopyright')
%
% The function is useful together with 'pcode', which generates pre-parsed
% files that hide the algorithms inside and therefore are suitable for
% distributing a proprietary software to third parties. However, the
% so-generated files do not contain help information. This can be fixed
% using extracthelp, as shown in the example below.
%
% Example
%  extracthelp         % create the .\phelp folder and the help files 
%                      % for all files in the current folder
%  pcode *.m           % convert all .M files to .P files
%  movefile('*.p','.\phelp')  
%       % moves the .P files to the \phelp folder the phelp folder contains
%       % the files you can now safely distribute
%
% See also  pcode

% Copyright 2009-2011        Andrey Popov
% This function is distributed under BSD license (see code for details).
% Original authors information:
% Andrey Popov          andrey.popov@gmx.net
% Last update: 07.11.2011

% Redistribution and use in source and binary forms, with or without 
% modification, are permitted provided that the following conditions are 
% met:
% 
%     * Redistributions of source code must retain the above copyright 
%       notice, this list of conditions and the following disclaimer.
%     * Redistributions in binary form must reproduce the above copyright 
%       notice, this list of conditions and the following disclaimer in 
%       the documentation and/or other materials provided with the distribution
%       
% THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 
% AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
% IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
% ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 
% LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 
% CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 
% SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 
% INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
% CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
% ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
% POSSIBILITY OF SUCH DAMAGE.

function extracthelp(nocopyright)

folder_name = 'phelp';          % change it if you want

eval(['mkdir ' folder_name]);
D = dir;

if nargin == 1 && strcmpi(nocopyright,'nocopyright')
    % Do not extract the copyrights (i.e., stop extracting comments at the
    % first non-commented line)
    CRb = sprintf('\n ');   CRe = sprintf('\n');    % carrige return

    for ind = 1:size(D,1)
        if ~D(ind).isdir
            DN = D(ind).name;
            if strcmpi(DN(end-1:end),'.m')
                F = fopen([pwd '\' folder_name '\' DN],'w+');
                B = help(DN);
                B = strrep(B,CRb,[CRe '%']);
                B = ['%' B(2:end)];
                fwrite(F,B);
                fclose(F);
            end
        end
    end
    
else
    % extract also the 2nd commented block (if there are no code-lines in
    % between)
   
    for ind = 1:size(D,1)
        if ~D(ind).isdir
            DN = D(ind).name;
            if strcmpi(DN(end-1:end),'.m')
                H = fopen(DN,'r');
                if H < 0
                    error('EH:fopenH','Could not open source file %s',DN);
                else
                    F = fopen([pwd '\' folder_name '\' DN],'w+');
                    if F < 0;
                        fclose('all');
                        error('EH:fopenF','Could not open destination %s',DN);
                    else
                        A = fread(H);
                        ma = length(A);
                        a1 = find(A==37,1,'first');

                        if a1 ~= 0      % i.e., there are comments
                            aind = find(A==13);
                            one_free_line = false;
                            ae = ma;

                            for ind = 1:length(aind)
                                if     A(min(aind(ind)+2,ma)) == 37
                                elseif A(min(aind(ind)+2,ma)) == 13
                                    if ~one_free_line
                                        one_free_line = true;
                                    else
                                        % end of comment block + copyright
                                        ae = aind(ind);
                                        break
                                    end
                                else
                                    % normal end of comment block
                                    ae = aind(ind);
                                    break
                                end
                            end

                            B = A(a1:ae);
                            fwrite(F,B);
                        end
                    end
                end
                fclose('all');
            end
        end
    end
end

end

Contact us at files@mathworks.com