Code covered by the BSD License  

Highlights from
Doctest - embed testable examples in your function's help comments

Doctest - embed testable examples in your function's help comments

by

 

27 Sep 2010 (Updated )

Put a usage example in the help of your function, then test it to make sure it still works over time

add3(value)
function sum = add3(value)
% adds 3 to a number
%
% add3(value)
%    returns (value + 3)
%
% Examples:
%
% >> add3(7)
% 
% ans =
% 
%     10
% 
% >> add3([2 4])
% 
% ans =
% 
%      5     7
% 
% >> add3('hi')
% ??? Error using ==> add3 ***
% add3(value) requires value to be a number
% 
%
% TWO blank lines before the prose description of the function continues
%


if ~ isnumeric(value)
    error('add3(value) requires value to be a number');
end

sum = value + 3;

Contact us