Debugging Functions

Matlab has an extensive debugger that allows you to examine what is going on inside a function when you encounter problems with it. If you type "help debug" at the matlab prompt, it will list all of the debugging commands available.
  Debugging commands.
    dbstop     - Set breakpoint.
    dbclear    - Remove breakpoint.
    dbcont     - Resume execution.
    dbdown     - Change local workspace context.
    dbstack    - List who called whom.
    dbstatus   - List all breakpoints.
    dbstep     - Execute one or more lines.
    dbtype     - List M-file with line numbers.
    dbup       - Change local workspace context.
    dbquit     - Quit debug mode.

Normally, you would enter the debugger by setting a breakpoint either at the beginning of your function (dbstop in functionname) or at the point where an error first occurs (dbstop if error).
help dbstop
lists some other ways to set a debugging stop to enter debug mode. When it "stops" at the breakpoint, you will be in the debugger. The prompt looks like K>> instead of >>. The debugger indicates the next line it will run.

When it's printed a line out, it has not yet run that line. It will run that line the next time you type "dbstep" to step down another line in the function.

Then you can examine the variables in the function, try to determine what might have the wrong value, do logical tests, and step through the function one line at a time using "dbstep" to see what the function is doing step-by-step.