Comments before the H1 line in help text
Show older comments
Is it possible to specify where exactly the help text begins in an m-file? OR, is there a way to put a comment at the beginning of an m-file that won't get picked up when I ask for the help text on the command line?
Context: If there's some boilerplate that I absolutely need to put at the beginning of my files, The former will show up instead of the desired help text:
% Boilerplate text << what gets returned, but I can't move it
%FOO An example function. << what I'd like to return
function [] = foo()
% Does something
end
Thanks!
3 Comments
DGM
on 13 Jul 2023
Who needs to use these files? If it's only for yourself, then you can make a wrapper for help(). If you're trying to make your file synopsis usable for other people, then making some bespoke help() wrapper or replacement isn't much of a solution.
Bear in mind that the optional text output from help() won't support the inline links help would normally provide. You'd have to dig into the undocumented tools behind help() in order to see if you can get to that. Things appear to have changed since R2019b, so any patchjob I make is likely to break in newer versions.
Alternatively, just leave the boilerplate attached to the top of the synopsis like a giant looming monument to unquestionable rules as artificial impediments.
Walter Roberson
on 13 Jul 2023
I just dug into the code, and no there is no option in the code to extract anything other than starting from the first comment.
Frank Pesta
on 13 Jul 2023
Accepted Answer
More Answers (1)
Image Analyst
on 13 Jul 2023
% Test code for PrintHelp
filename = 'foo.m';
PrintHelp(filename)
% function to print the first non-boilerplate batch of comments.
function PrintHelp(filename)
allLines = readlines(filename);
% Skip first batch of comments and look for the blank space
startsWithPercent = startsWith(allLines, '%');
firstLineOfDescription = find(startsWithPercent == 0, 3) + 1;
startsWithFunction = find(startsWith(allLines, 'function'), 1, 'first');
description = allLines(firstLineOfDescription(1) : (startsWithFunction - 1));
fprintf('===================================================================\n');
fprintf('Help for %s:\n', filename)
for k = 1 : numel(description)
fprintf('%s\n', description(k));
end
fprintf('===================================================================\n');
end
Prints to the command window this:
===================================================================
Help for foo.m:
%FOO An example function. << what I'd like to return
% stuff1
% stuff2
===================================================================
Categories
Find more on Entering Commands in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!