pgmank
pgmank

Reputation: 5819

Difference between help func and help(func) in Octave

The help function works differently when used as help func compared to help(func).

For example, executing help mvnrnd, displays the desired output:

>> help mvnrnd
'mvnrnd' is a function from the file /home/student/octave/statistics-1.4.0/mvnrnd.m

 -- Function File: S = mvnrnd (MU, SIGMA)
 -- Function File: S = mvnrnd (MU, SIGMA, N)
 -- Function File: S = mvnrnd (..., TOL)
     Draw N random D-dimensional vectors from a multivariate Gaussian
     distribution with mean MU(NxD) and covariance matrix SIGMA(DxD).

     MU must be N-by-D (or 1-by-D if N is given) or a scalar.

     If the argument TOL is given the eigenvalues of SIGMA are checked
     for positivity against -100*tol.  The default value of tol is
     'eps*norm (Sigma, "fro")'.

Additional help for built-in functions and operators is
available in the online version of the manual.  Use the command
'doc <topic>' to search the manual index.

Help and information about Octave is also available on the WWW
at http://www.octave.org and via the [email protected]
mailing list.

While executing help(mvnrnd), produces error:

>> help(mvnrnd)
error: 'Sigma' undefined near line 22 column 50
error: called from
    mvnrnd

I thought help func and help(func) where the same, but it seems that there are some differences which I couldn't find while searching online. Any ideas?

I have also tried creating my own function, for example:

function hello()
  % Whatsup
  print("hello")
end

And has the same behavior. Executing help hello works fine, whereas error occurs when executing help(hello). It is like it's trying to execute the code inside the function, because the stack trace has references to the code of the function and its calling functions. This is the output:

>> help(hello)
warning: print.m: fig2dev binary is not available.
Some output formats are not available.
warning: called from
    __print_parse_opts__ at line 388 column 9
    print at line 316 column 8
    hello at line 3 column 3
error: print: no figure to print
error: called from
    print at line 341 column 5
    hello at line 3 column 3

P.S: For mvnrnd to be accessible, the package statistics must be loaded:

pkg load statistics

Upvotes: 2

Views: 88

Answers (1)

Cris Luengo
Cris Luengo

Reputation: 60514

The MATLAB language seems to have functions (such as sum) as well as commands (such as help). However, it knows only functions. The "command syntax" is where you leave off the parentheses when calling a function. Whatever follows the function name is split at spaces and interpreted as strings, which are passed as arguments to the function. Therefore,

help mvnrnd

is the same as

help('mvnrnd')

(and note that in Octave, the double quote is equivalent to the single quote, so help("mvnrnd") would also be the same thing; in MATLAB the double quote has a different meaning).

You can read more about this syntax in this Q&A.

When you write

help(mvnrnd)

then Octave first interprets the argument to the function, mvnrnd, trying to run that function without any arguments (in the MATLAB language, mvnrnd is the same as mvnrnd()). If this didn't produce an error, it would call help with whatever output mvnrnd generates.

Let's try an experiment:

function out = myfunc
   % This is help for the function myfunc
   out = 'sum';
end

Now we see that:

>> help myfunc
'myfunc' is a function from the file /home/cris/octave/myfunc.m

 This is help for the function myfunc

>> help('myfunc')
'myfunc' is a function from the file /home/cris/octave/myfunc.m

 This is help for the function myfunc

>> help(myfunc)
'sum' is a built-in function from the file libinterp/corefcn/data.cc

 -- sum (X)
 -- sum (X, DIM)
 -- sum (..., "native")
 -- sum (..., "double")
 -- sum (..., "extra")
     Sum of elements along dimension DIM.

That is, in the 3rd case, myfunc returned sum, and now help shows the help for function sum.

Upvotes: 3

Related Questions