Matlab Editor

From NA-Wiki

Jump to: navigation, search

It has apparently been the case here in the NA group that people regard the Matlab GUI (on Solaris and Linux platforms) as big, slow and prone to crashing. There are still good reasons for running Matlab without the full desktop GUI, but one should definitely think twice before using Emacs instead of the Matlab Editor! I will make that case here by reminding of some of the features that the Matlab Editor provides.

Contents

First: Some options that you probably want to set

In the Preferences menu:

  • Smart indenting (under Editor/Debugger->Tab)
  • Emacs key bindings (under Keyboard) if you're used to Emacs.

MLint finds suspicious constructs in the code

MLint is a derivative of the old "lint" program for sun systems. It checks the source code for inconsistencies, errors, potential efficiency problems, bad style etc. It runs automatically in the editor, marking lines in the right margin with either warnings or errors. Some things that MLint complains about are perhaps not interesting (such as when it complains that it cannot determine whether x is a function or variable), but every single warnings it issues can be disabled (under Preferences->MLint). Also, the underlining of warnings can be disabled.

Some examples of MLint warnings:

  • Use && instead of & as the AND operator in (scalar) conditional statements
  • Global or persistent variable <name> appears never to be used or set in this function
  • Using ISEMPTY is usually faster than comparing LENGTH to 0
  • The value assigned here to variable <name> might never be used
  • <name> might be growing inside a loop. Consider preallocating for speed.
  • ACCUMARRAY([r,c],v,[m,n]) is faster than FULL(SPARSE(r,c,v,m,n))

Even though you probably are a Matlab boffin, some of these warnings usually helps you improve the code. Note that MLint can be used from the command line, but it is not as convenient.

Debugger

The debugger can be used from the command line, but it is very cumbersome and unproductive (even worse than using MLint from the command line). What does the debugger do? It allows you to halt the program at any line to inspect the state of the program. The lines where execution halts are called breakpoints.

Breakpoints and stepping

The debugging process can go something like

  • In the Matlab editor one sets breakpoints by clicking in the left margin (on the short horizontal line next to the line number).
  • Run the program. When it reaches a breakpoint, control of the command line is returned to the user but the prompt is changed to K>> . Inspect the variables as usual (maybe plot something, or just inspect array dimensions with whos). One useful command is dbstack, which displays all functions on the stack (i.e, it shows how it got to the breakpoint).
  • To proceed from a break point:
    • Step one line: Press F10 in the editor window, or type dbstep
    • Continue until next breakpoint: F5 (dbcont)
    • Terminate the program: Shift+F5 (dbquit)

Debugging using the Matlab debugger is much more efficient than inserting disp-statements etc.

More advanced debugging tips

Jumping in the stack

From the debugging prompt you can do

K>> dbstack
> In ppval at 72
  In spline at 100
  In interp1 at 279

to get the stack dump. This shows how the program reached this breakpoint (i.e. the chain of functions that were called). Furthermore, one can jump in the stack by using the dbup and dbdown commands to get a better understanding of how the functions on the stack were called.

Always stop on 'inf' or 'nan'

It is possible to have the execution of a Matlab program stop and enter debug mode if it encounters a NaN or inf at any calculation. This is significant since your numerical calculations are probably unstable if you get a NaN or inf in an array. To enable this do

>> dbstop if naninf
>> (run some code)

To unset this flag, do

>> dbclear if naninf

Always stop on error/warning

The debugger may be set up so that Matlab halts and enters the debugging console as soon as an error or warning is thrown! One can then easily examine why the error/warning occurred instead of just getting the crash dump. To enable this do

>> dbstop if error

or

>> dbstop if warning

Disable these as above.

Other features

  • From the editor it is simple to publish your script to html and get help with cell mode formatting (File->Publish). Publishing from Matlab is a topic in itself on this wiki.
  • Cell mode lets you select a region of a script to execute (if you for some bizarre reason should need to).
Personal tools