Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get old-style help in Matlab's command window

Tags:

matlab

Short version of question

In recent versions of Matlab (I have seen it in R2014b and R2015a on Windows), when you type help foo you get a brief description of the function and its signatures. For example, typing help bsxfun produces something like this (only with better format):

This MATLAB function applies the element-by-element binary operation specified by the function handle fun to arrays A and B, with singleton expansion enabled.

C = bsxfun(fun,A,B)

Reference page for bsxfun

See also arrayfun, repmat

Other uses of bsxfun distcomp/bsxfun

This is of course only a summary of the actual documentation. To get the full documentation you need to type doc foo. This opens the HTML help browser, which takes quite some time (at least on some computers).

Is there a way to get the full help in the command window (thus avoiding the help browser), as it used to be in older Matlab versions?

Long version of question

To look into this in more detail, I'll define "old" Matlab versions as those that don't have HTML help, and "new" versions as those that do. I also need to give each type of help a name, in order to refer to them:

  • FP (Full, Plain): full help in the form of plain text, shown in Matlab command window (old style).

  • SH (Summarized, HTML): summarized help in the form of HTML, shown in Matlab command window.

  • FH (Full, HTML): full help in the form of HTML, shown in the help browser.

As is well known, the text for FP help is contained in the first comment lines in the file defining the function. In new Matlab versions, functions may also have an associated HTML file. This file contains SH help in an HTML tag, and FH help in HTML code.

Possible behaviour is:

  • In old Matlab versions, help foo produced FP help.
  • In new Matlab versions, help foo produces SH help if foo has an associated HTML help file, and FP help if it doesn't.
  • In new Matlab versions, doc foo produces FH help if foo has an associated HTML help file. If it doesn't, FP help is shown in the help browser (without format).

So the problem is more properly phrased as: how to show FP help in new Matlab versions when foo has an associated HTML help file. The question is meaningful because

  • Most Matlab functions do have an associated HTML help file.
  • Most Matlab functions, even built-in functions (that have no m-code), have and m-file containing FP help.

An additional motivation is that in some cases the FP documentation contains features that don't appear in the FH documentation (see for example here).

like image 960
Luis Mendo Avatar asked Mar 09 '15 23:03

Luis Mendo


People also ask

How do I get the Command Window back in MATLAB?

To restore the Command Window to the default location and size, go to the Home tab, and in the Environment section, click Layout. Then, select from one of the preconfigured desktop layout options.

What is the fastest way to get help from a function in MATLAB?

Select a function name in the Editor, Command Window, or Help browser; right-click; and then select Help on Selection. After you type an open parenthesis for function inputs, pause or press Ctrl + F1. Use the help command. Click the function icon to the left of the command prompt.

How do you clear old codes in MATLAB?

Swipe left on the command you want to remove. Tap Delete.

How do you add help in MATLAB?

Help text appears in the Command Window when you use the help command. You also can use the doc command to display the help text in a separate browser. Create help text by inserting text at the beginning of the file, immediately before the function definition line (the line with the function keyword).


1 Answers

Original answer (Matlab versions R2014b, R2015a)

Although the documentation doesn't tell, the help function in these Matlab versions supports zero, one or two output arguments. You can check this typing open help and looking at the function signature:

function [out, docTopic] = help(varargin)

In essence, help works internally as follows:

  1. It creates an object called process, of class helpUtils.helpProcess, by calling the class constructor as:

    process = helpUtils.helpProcess(nargout, nargin, varargin);
    

    where nargout, argin and varargin are those with which help was called.

  2. It runs the method process.getHelpText, which calls the undocumented, built-in function helpfunc, and as a result sets the property process.helpStr. This property contains the help string which is shown in the command window.

As it turns out, at least on Windows, depending on the value of nargout (which gets passed to the constructor helpUtils.helpProcess) the resulting help string will be FP or SH. Namely, it will be FP if nargout>0, and SH if nargout==0. You can check this by typing the following code (adapted from help.m) directly in the command window:

process = helpUtils.helpProcess(1, 1, {'bsxfun'});
process.getHelpText
process.helpStr

This will produce FP help. On the other hand, changing the first 1 (which corresponds to nargout in the actual call) into a 0,

process = helpUtils.helpProcess(0, 1, {'bsxfun'});
process.getHelpText
process.helpStr

will produce SH help.

I don't know why this is so, that is, how it works on a deeper level than this. All I know is that the getHelp method calls the undocumented helpfunc, which is at least involved in producing FP help.

So, to get FP help you need to call help with one or two output arguments. For example,

str = help('foo')

assigns the FP help text to variable str and displays it. Or you can use

disp(help('foo'))

which also has the effect of calling help with an (implicit) output argument.

To have this behaviour from the standard command help foo, you could define a help function to override Matlab's help, and place it in your Matlab document folder. This folder normally appears first in the path (or you can make sure it does by editing startup.m), and thus has precedence. The new function will essentially call Matlab's help with one output argument, and then display the resulting (FP) help text. In order to call the overriden function it is necessary to temporarily change to its folder:

function help(varargin)
if isempty(varargin)
    varargin = {'help'}; %// `help` should be equivalent to `help help`
end
d = pwd; %// take note of current folder
cd(fullfile(matlabroot, 'toolbox', 'matlab', 'helptools')) %// folder where the
    %// standard `help` function is
disp(help(varargin{1}));
cd(d) %// restore folder

So now, finally, help foo produces the old-style (FP) help.

Edit for Matlab version R2015b

In Matlab R2015b the behaviour seems to have changed for the better. Typing help foo no longer produces SH help. It's not exactly FP either. In fact it's better than that: it produces FH help but in the command Window, not in the browser. Or, equivalently, it produces FP help but with links and better formattting.

So no need to tweak anymore!

Edit for Matlab version R2018a

Matlab R2018a again gives SH help. The solution provided in this answer works (that is, produces FP help).

So back to tweaking!

like image 134
Luis Mendo Avatar answered Sep 21 '22 02:09

Luis Mendo