Join Patrick Royal for an in-depth discussion in this video Debugging, part of Learning MATLAB.
Even the best coder, is unlikely to write a script that runs perfectly the first time, and coding in MATLAB is no exception. To help with debugging, MATLAB provides several different methods of getting a closer look at the mechanics of your functions as they run so that the problem can be identified. The simplest step to take when a bug is encountered is to remove the semicolons from every line in the suspect area. This causes MATLAB to display the output from every single calculation, potentially allowing you to spot the problem.
In fact, unless you have a long loop with lots of data that can't be displayed easily, there's no reason not to leave off semicolons entirely, at least until the program is working to your specifications. If that doesn't solve the problem, the next step is to run the program in sections by specifying breakpoints. Breakpoints can be added from within the Editor Window, so we'll open up the practice script that we worked on in an earlier video, so we can add them now. Breakpoints are added in the Breakpoints drop down menu. To set a breakpoint on a certain line, click on the drop down menu and then click Set/Clear breakpoint.
In this example, I'll set a breakpoint right after the first line so that the program will pause before it tries to calculate the mean of the stock. Now when the program runs, it will automatically stop at the indicated line and reopen the function. From here, you can see exactly the current state of the function, so if a variable is out of place, you could find it. In this case, because we put a break point right after reading in the file, it would be easy to see if there was an error with the file reading, or if the file could not be found, or if some other problem manifested.
When ready, you can click Step to run the next line of the function, or Continue, to continue function as normal until the next break point. Once the break point is set, it can be enabled or disabled in the same dropdown menu, so you can just add break points at critical sections of your script from the beginning, and simply enable them whenever a problem is encountered. Conditional breakpoints are a little bit more complicated. In this case the breakpoint will only activate if a specific condition is met. If you know that a variable is taking on an inappropriate value at some point in the function but you're not sure when a conditional breakpoint can help you find the exact point of the divergence. These breakpoints are also useful if you want to break at a specific iteration of a loop.
Rather than every time the loop executes. The final settings that can aid in debugging are the error handling codes. By default, a program will continue to execute if it runs into a warning or completely quit if it returns an error. Instead, checking the Stop on Error or Stop on Warning options will cause the program to treat the error like it's a break point and just pause. The advantage of this is that the state of the program at the time when the error occurred is saved, so it can be easier to find the cause. Between line by line output, breakpoints and error handling, finding an error in MATLAB should be as pain free as possible.
- Installing MATLAB
- Working with MATLAB variables
- Working with matrix and scalar operations
- Creating functions
- Understanding performance considerations
- Building basic plots
- Creating responsive programs
- Editing variables manually
- Working with the Statistics Toolbox