hi there and welcome to this video on debugging matlab code in this video we will learn about
the 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 the 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 tools which we'll 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 zero and one so zero plus one is one one plus one is two one plus two is three two plus three is
five etc note that the 10th fibonacci number is 34 which we will try to calculate with code we want
the 10th fibonacci number we're going to start with 0 and 1 and at every iteration we're going
to add a new fibonacci number to this array that we have and we're going to return the last element
to start out we can look at 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 the 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 to run by pausing on errors which will put you in
the debugger mode when the error occurs and you'll 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 the 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
because we use the pause on error setting and we have encountered this runtime error
we are now in the debugger mode we can tell by the change in the editor tab at the top
the green arrow on the left and the k in the command window at the bottom
to start debugging we can set breakpoints in our code by clicking the line number
which creates a red dot when the code runs it will stop at this break point the green
arrow represents what line we are on and it stopped on line 9 which is where the 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 just 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 on the first iteration and that makes sense because in
the first iteration we tried to add fib array of one and fib array of zero since i was one
fib array of zero does not exist since matlab is a one index language so let's change i to
start at two 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 break point inside the for
loop since that is where the fibo variable of interest is constantly being updated
we can click continue to iterate through the for loop at the end we notice the iteration is
one 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 one iteration testing our code now we get the right answer
in summary we have learned about the types of bugs that you can encounter while writing matlab code
and how 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