Comments before the H1 line in help text
4 views (last 30 days)
Show older comments
Frank Pesta
on 12 Jul 2023
Commented: Frank Pesta
on 13 Jul 2023
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
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.
Accepted Answer
Image Analyst
on 12 Jul 2023
I don't believe so. It does not look like it. The help function just takes all the comments at the beginning of the file. If you want it to skip some comment lines and spaces, then I think you'd have to write your own help function that had a "SkipLines" option or somehow found the help comments immediately prior to the function, skipping any boilerplate text and blank lines at the beginning. However, I would call that function something else, not help.m so you don't confuse MATLAB at other times when the standard help function may be needed.
5 Comments
Image Analyst
on 13 Jul 2023
Like I said and demonstrated below in my answer, it would be easier to use readlines than parsing what help returns (a single long string) if you want to extract out just certain lines of the file.
DGM
on 13 Jul 2023
I would think that an ideal wrapper/replacement would have behavior identical to help() in the cases where no custom processing is required. That's why I would expect investigation of how help() operates (e.g. how it adds docs links)
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
===================================================================
0 Comments
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!