Transcription

 

As we start combining many lines of code

into scripts or functions,

there are more opportunities to introduce mistakes.

Sometimes our code will result in an error message,

or it may produce an output that is

different than we expect.

Oops!

Such errors are often called bugs.

We can locate mistakes and fix errors

using the debugger in MATLAB.

Let's see how using this example.

This code is meant to calculate the total account

balance in interest earned on a vector

of deposits and interest rates for each year.

However, when we run the code

we receive an error message.

Hmm.

There was a problem in line 7 of the code

related to multiplication as referenced

in the error message.

To help determine the cause of the error,

let's run the code again,

but this time pause the execution of the code

at the line where the error occurs.

We do this using a breakpoint.

With the code file open in the MATLAB editor,

let's create a break point by left clicking

on the dash next to line 7.

A red circle is displayed on the line number

indicating that we have set a breakpoint.

Now when we run the file

the command prompt changes to show us

that we are in debug mode,

and new buttons are available

in the MATLAB editor toolstrip.

We also see the current values

of variables in the workspace.

Notice that the loop variable, i, is one,

and variable interest is zero because

the code was stopped before line 7 was executed.

We have two options for continuing code execution.

We could use the "Continue" button

to resume execution until another breakpoint is reached,

an error occurs, or the code runs to completion.

Or we can use the "Step" button to execute

only the current line of code

which is indicated by the green arrow.

Since we don't know where the error is,

let's use the step button to execute line 7

and advance the code one line.

The variable interest is updated and we see

it is a vector of six elements!

We meant for interest to be a scalar value,

so it looks like we might have found our error.

In debug mode, we can type commands to

test possible corrections.

Here, what we meant to do was use the loop variable, i ,

to select a value from the vector rates.

Since we found the error, let's press

the "Quit Debugging" button,

then update and save the code file.

It's important to quit debugging because

we can't save our changes in debug mode.

Notice the breakpoint wasn't removed,

and execution will pause here next time the code is run.

To run the code without stopping,

we removed the breakpoint by left clicking on it.

Now let's run the code and...

oops, another error?

Do you see where the error is?

If not try using the debug tools

to find and fix the mistake.

Set a breakpoint and "Step" or "Continue"

through the code until you find the error.

Pay attention to the dimensions of variables

as the code iterates through the FOR loop.

 

Writing Functions Robotics Fundamentals

Thiago Carlos
Module by Thiago Carlos, updated more than 1 year ago
No tags specified