Create help for method with arguments

38 views (last 30 days)
Vasco
Vasco on 21 Mar 2022
Moved: Matt J on 17 Jan 2026 at 13:57
I like to document my functions, such that on pressing on F1, it shows description of the function, and its parameters.
The recently introduced arguments block allows to define arguments sizes and types. What is the best practice to document these arguments?
I preferably like to keep the defintion and the documentation at the same location, like in the example below:
function [m,s] = twoStats(x)
% Test for help
arguments
x (1,:) {mustBeNumeric} % Argument 1, the array to calculate statistics on
end
m = mean(x,"all");
s = std(x,1,"all");
end
Preferred output:
doc twoStats
Test for help
Arguments:
- x: Argument 1, the array to calculate statistics on

Accepted Answer

Matt J
Matt J on 16 Jan 2026 at 15:45
Moved: Matt J on 17 Jan 2026 at 13:57
The Quick Access Toolbar function favoriteArgs2Help.m, now available in this FEX submission,
will reformat and transfer the arg block info to help text.

More Answers (1)

Matt J
Matt J on 21 Mar 2022
function [m,s] = twoStats(x)
% Test for help
%
%Arguments:
% - x: Argument 1, the array to calculate statistics on
arguments
x (1,:) {mustBeNumeric} % Argument 1, the array to calculate statistics on
end
m = mean(x,"all");
s = std(x,1,"all");
end
  2 Comments
Vasco
Vasco on 21 Mar 2022
Ok, this seems the trivial solution, just copy paste the information from the arguments block to the documentation block. However, now the documentation and definition is split to different places.
Matt J
Matt J on 21 Mar 2022
Edited: Matt J on 21 Mar 2022
There is no other solution. The help text can only be in one place. You could of course, write tools that will semi-automate the documentation process. Once you have the argument text copied to the clipboard, code like the following can be used to parse it into the format you want:
>> contents = clipboard('paste')
contents =
'x (1,:) {mustBeNumeric} % Argument 1, the array to calculate statistics on'
>> out = "% - "+extractBefore(contents,' ')+" "+extractAfter(contents,'%'); disp(out)
% - x Argument 1, the array to calculate statistics on

Sign in to comment.

Categories

Find more on Install Products in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!