Comments before the H1 line in help text

4 vues (au cours des 30 derniers jours)
Frank Pesta
Frank Pesta le 12 Juil 2023
Commenté : Frank Pesta le 13 Juil 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 commentaires
Walter Roberson
Walter Roberson le 13 Juil 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
Frank Pesta le 13 Juil 2023
Thanks folks; that's what I was afraid of. I was holding out hope that there was some undocumented help() functionality similar to python docstrings that I wasn't yet aware of.
Yes, I could wrap help(), but I'd think that would open the door to further unintended consequences downstream. For my paricular case I'll just leave the boilerplate per DGM; it'll remain a minor inconvenience as opposed to wrapping around base MATLAB behavior for the sake of pretty appearance whilst possibly causing other issues.

Connectez-vous pour commenter.

Réponse acceptée

Image Analyst
Image Analyst le 12 Juil 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 commentaires
Image Analyst
Image Analyst le 13 Juil 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
DGM le 13 Juil 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)

Connectez-vous pour commenter.

Plus de réponses (1)

Image Analyst
Image Analyst le 13 Juil 2023
You could try using readlines and writing your own help function PrintHelp. Something like this:
% 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
===================================================================

Catégories

En savoir plus sur Scripts dans Help Center et File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by