Video length is 5:07

How to Debug MATLAB Code

Learn about the built-in MATLAB® debugger tools that help catch bugs while coding. Discover the types of bugs you may encounter while coding, then see how to use MATLAB to examine error messages and use the debugger tools (breakpoints, step, continue, etc.) to identify where bugs are occurring.

To illustrate these debugging techniques, this demonstration uses code for calculating the 10th Fibonacci number. You’ll see an example of the types of errors, and you’ll learn how to correct them based on what is present in the MATLAB user interface. You’ll also get an overview of other best practices for debugging settings.

Published: 10 Sep 2024

Hi there, and welcome to this video on debugging MATLAB code. In this video, we will learn about types of bugs while coding, the MATLAB debugger tools, and we will end on a demonstration of these tools using the Fibonacci sequence as an example.

To start, a software bug is an error in a computer program that causes an incorrect or unexpected result. There are three types of errors. The first is a syntax error which is when the program cannot compile or run. An example of a syntax error could be an incorrect punctuation or misspelling of a variable name.  The second is a runtime error which is when the error occurs while the program is running. An example could be accessing an invalid element in an array or an infinite loop in your code. The last type of error is a logical error which is when your code runs to completion, but the result is not correct. An example could be returning the wrong Fibonacci number.

To start debugging, typical methods include examining variable values, backtracking the error message, and using the MATLAB debugger tool which we will dive into now.

The following is some Fibonacci sequence code that we will need to debug. The Fibonacci sequence is a series of numbers where the number is the addition of the last two numbers starting with 0 and 1. So, 0+1=1, 1+1=2, 1+2=3, 2+3=5, etc. Note that the tenth Fibonacci number is 34, which we will try to calculate with code. We want the 10th Fibonacci number. We are going to start with 0 and 1, and at every iteration we are going to add a new Fibonacci number to this array that we have, and we are going to return the last element.

To start out, we can look on the right side of the editor or in the code and see that there are red and yellow marks. The red marks indicate a syntax error, and the yellow marks are warnings that do not cause errors but can help improve the code efficiency. After double checking line 9 from the error message, let’s fix this syntax error of a missing parenthesis here.

Now that we don’t have any red error marks, we can run the code by clicking Run. Typically, a good practice is run by pausing on errors, which will put you in the debugger mode when the error occurs, and you will know exactly where the error occurred.

Notice the error message that we get this time after running. It is telling us that we don’t have this variable defined, and based on the line number, we can tell that we are spelling Fibonacci incorrectly. Let’s fix that syntax error and try again.

Since we used the “Pause on Errors” setting, and we have encountered this runtime error, we are now in debugger mode. We can tell by the change in the editor tab at the top, the green arrow at the left, and the K in the command window at the bottom.

To start debugging, we can set breakpoints in our code by clicking by the line number which creates a red dot. When the code runs, it will stop at the breakpoint. The green arrow represents what line we are on, and it stopped on line 9 where our error is.

At the top of the Editor tab, we can clear and set breakpoints and move through the code. The power of the debugger is that without fully executing code we can examine intermediate steps and variables so we can deduce where the problem could be. To do so, the most commonly used commands are step and continue where step executes the current line in the file and continue resumes execution until completion or until another breakpoint is encountered.

We can step in and step out of lines that call other functions to get a better idea of what is going on for each line. This will add to our function call stack. During debugging you can use the command window to make plots, examine variables, or just do about anything you normally would but it would be for the current state of the program not fully executed. When you are done, you can click quit debugging in the top right.

For this runtime error, the message tells us that “Array indices must be positive integers or logical values.” and we notice that our fib array variable was not updated at all as seen in the workspace variables. This tells us that it failed in the first iteration, and that makes sense because in the first iteration we tried to add the fib array of 1 and fib array of 0 since I was 1. Fib array of 0 does not exits since MATLAB is a 1 indexed language, so let’s change I to start at 2 and run the code again.

This time, we notice the code runs to completion, but the incorrect number is returned. This is a logical error, so let’s put a breakpoint inside the for loop since that is where the fib array variable is constantly being updated. We click continue to iterate through the loop.

At the end, we notice the iteration is 1 too many as seen in the size of the fib array and the extra iteration so we can fix this by decreasing our loop by 1 iteration. Testing our code now, we get the right answer.

In summary, we learned about the types bugs when writing MATLAB code and how to use the debugger tools to fix these bugs. Some key takeaways are to use the built in debugger, examine your workspace variables, and carefully read the red underlines and error messages that appear while you are coding. Please check out this documentation below for more detailed and advanced help. Thank you!

Related Products

Learn More