Table of Contents

Matlab debugging

This page gives a quick introduction into how to locate errors in Matlab functions (including those of the NeuroElf toolbox, I never claimed the toolbox is 100 per cent error free 8-) !)

Motivation

While reviewing and testing code is part of the development and implementation process, it still is a common phenomenon that some part of the code that used inside a function or batch file creates an error (usually aborting the current computation and returning to the Matlab prompt with a sometimes cryptic message) or produces an erroneous result (e.g. the result of a specific operation is known but the function that should provide that result delivers a different one instead).

It is often complicated to interpret an error message correctly and find the problematic passage in the code, and this page should give some first pointers about how to do this.

Requirements

Matlab must be started with Desktop and Java VM enabled to fully use the procedures mentioned here. Also, you should familiarize yourself with the commands (and also keyboard shortcuts!) associated with the most important Matlab debugging functions.

Command overview

The following commands can be employed to locate erroneous code:

General procedure

In case of an error...

If a function (or batch file) produces an error (error message), the first things to do are

In case of a wrong result...

This is a much trickier situation. If you don't have any clear suspicion about what is happening, it is useful to follow the arguments you pass into a function (or that are created by batch files) and first locate the approximate location of an error. For invalid results, a typical thing to do is to check whether variables contain bad values:

% test if variable timeseries contains problematic values
any(isinf(timeseries(:)) | isnan(timeseries(:))

Such bad values very commonly disrupt normal behavior of functions.

A last resort

If you are unable to locate the error and the function you suspect is causing the problem is part of NeuroElf, please feel free to get in touch with me. In that case, others can most likely benefit from this by having more reliable functions in an upcoming version of the toolbox!