keyboard & dbstop – MATLAB

Finally I would like to start off with a few Matlab tips as promised earlier. We used Matlab extensively during our 3rd year internship and found that a lot of general file-handling, and OS related tasks can be done too.

But today, it shall be on a programmers requirement. During the process of actually writing code [not downloading and running it :)] one may encounter errors. Probably with dimensions, etc.

The keyboard command is a simple way to stop the execution and give the coder a chance to check the values on the MATLAB command window whose cursor turns to a K>> or even write different code from that of the execution sequence. Its especially useful in functions in which we generally have no chance of seeing what exactly are the values of the internally used variables. return continues with the normal execution with updated variables (if any).

Another similar function is the dbstop which adds a bit more functionality. Typically useful is the set of “dbstop if …” commands of which dbstop if error stops and gives you power to debug (db) only in case you encounter an error. dbcont continues, dbquit exits, dbstep moves to the next execution, etc.

This can be also achieved in the Editor by clicking on the horizontal line and making it a red ball which inserts a breakpoint and then using the icons of step which carries a line of execution, step in which goes in the function (in case its a function), step out which comes out of that function or finishes in case its the main or continue to continue and exit debug.

In fact even to understand downloaded code, its nice to let it execute step-by-step and see how the variables are changing to get an insight into it.

Coming up with a lot more Matlab tricks soon. 🙂


One thought on “keyboard & dbstop – MATLAB

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s