TExcelTAD
Home › MATLAB / Octave › Help & Documentation

Help & Documentation

MATLAB and Octave ship with thousands of functions, and nobody remembers all their options. Knowing how to look things up quickly is part of being productive.

help and doc

Type help followed by a function name in the Command Window to print a short description, the calling forms and related functions. doc opens the full documentation page with examples (in Octave, doc opens the HTML/Info manual):

text
>> help linspace
 linspace  Linearly spaced vector.
    Y = linspace(X1, X2) generates a row vector of 100 linearly
    equally spaced points between X1 and X2.
    Y = linspace(X1, X2, N) generates N points between X1 and X2.
    ...
>> doc linspace

The wording above is shortened; MATLAB's and Octave's help texts are different, but both list every calling form. Online, the MATLAB documentation lives at mathworks.com/help and the Octave manual at docs.octave.org.

Command Use it to
help name Read a function's help text in the Command Window
doc name Open the full documentation page
lookfor word Search the first help line of every function for a word
which name See where a function or file comes from
exist name Check whether a name is a variable, file, or built-in
type name Show the source code of a function file

Checking names from code

exist returns a code that tells you what a name refers to. Use it to avoid clashes before naming a variable or file:

Code Meaning
0 Not found
1 Variable in the workspace
2 File (including .m files on the path)
5 Built-in function
matlab · try it yourself
total = 10;
names = {'total', 'sin', 'fliplr', 'no_such_thing'};
for k = 1:numel(names)
    fprintf('%-14s exist = %d\n', names{k}, exist(names{k}));
end
total          exist = 1
sin            exist = 5
fliplr         exist = 2
no_such_thing  exist = 0

sin is compiled into the program (code 5), while fliplr is written in the MATLAB language and lives in a .m file (code 2). Exact codes for some functions can differ between MATLAB and Octave, because each implements different functions as built-ins.

Asking a function how it can be called

nargin and nargout called with a function handle report how many inputs and outputs a function declares. (A negative number would mean "variable": the function accepts optional extra arguments through varargin.)

matlab · try it yourself
fprintf('boxVolume: %d inputs, %d output\n', nargin(@boxVolume), nargout(@boxVolume));
fprintf('anonymous: %d inputs\n', nargin(@(x, y) x + y));

function v = boxVolume(w, h, d)
v = w * h * d;
end
boxVolume: 3 inputs, 1 output
anonymous: 2 inputs

You'll learn about function handles (@name) and local functions at the end of a script in the Functions section.

Writing help for your own functions

The comment lines directly after a function line (or at the top of a script) are its help text. The first line, called the H1 line, is what lookfor searches, so make it a one-line summary:

matlab
function c = celsius(f)
%CELSIUS  Convert a temperature from Fahrenheit to Celsius.
%   C = CELSIUS(F) returns (F - 32) * 5/9. F may be an array.
%
%   Example:
%       celsius([32 212])   % returns [0 100]
%
%   See also FAHRENHEIT.
c = (f - 32) * 5 / 9;
end

Saved as celsius.m, the command help celsius prints those comment lines. Writing help like this costs a minute and saves time for everyone who uses the function later, including you in six months.

matlab · try it yourself
c = celsius([32 212 98.6]);
fprintf('%.1f ', c);
fprintf('\n');

function c = celsius(f)
%CELSIUS  Convert a temperature from Fahrenheit to Celsius.
c = (f - 32) * 5 / 9;
end
0.0 100.0 37.0 

Reading error messages

When something goes wrong, the error message usually names the problem and the line number. Read it before anything else. Some common ones:

Message (MATLAB wording) Usual cause
Undefined function or variable 'x' Typo, or the variable was never assigned
Index exceeds the number of array elements Reading past end
Arrays have incompatible sizes for this operation Element-wise operation on mismatched sizes
Incorrect dimensions for matrix multiplication Used * where .* was meant
Not enough input arguments Called a function with too few inputs

You can also inspect an error from code by catching it:

matlab · try it yourself
try
    result = undefinedThing + 1;
catch err
    disp(err.message)
end
'undefinedThing' undefined near line 2, column 14

That is Octave's wording; MATLAB says Unrecognized function or variable 'undefinedThing'. The messages of built-in errors are one of the places where the two programs differ, so don't write code that depends on their exact text.

Other places to get help

  • The examples at the bottom of each documentation page are often the fastest way to learn a function.
  • MATLAB Answers (mathworks.com/matlabcentral/answers) and the Octave Discourse forum have answers to most beginner questions.
  • MATLAB Onramp is a free interactive introduction from MathWorks, and this course follows a similar order.

Test yourself

Which command prints a function's help text in the Command Window?

  1. man name
  2. help name
  3. info(name)
  4. ?name

In your own function file, where does the help text come from?

  1. A separate .txt file
  2. The comment lines right after the function line
  3. The function's name
  4. A help keyword inside the function

What does exist('myVar') return when myVar is a workspace variable?

  1. 0
  2. 1
  3. 2
  4. 5