Generally, there are two problems you might have with your code: 1) It has an error and will not run or 2) it does not do what you want it to. We will address the first type of problem in this lesson. For the second problem, you will have to complete the rest of the course!
There are two general types of messages MATLAB may return to alert you to a problem (or potential problem) it sees in your code. These are called error and warning messages, and MATLAB will display them in the Command Window (see Figure 1).
Error messages will either not let your program run or stop it before it finishes running: it depends on the error. Error messages can identify errors in syntax, problems with the way you are calling MATLAB functions, etc.
Figure 1 shows an example of an error in MATLAB. As you can see, our program did not execute any lines of code. We know this because we can see our first line of code, which is written correctly, did not display any text to the Command Window. Also, notice MATLAB displays the error message in the Command Window, and tells us exactly what is wrong.
A warning is MATLAB’s way of telling you something about your program without stopping it from running. One example of this is mathematical alerts where MATLAB has checked something behind the scenes and is telling you to be careful (see Figure 2). This is done by the programmers writing that MATLAB function to alert you to a specific mathematical problem rather than a programming specific one. Another example is an outdated or “depreciated” code warning. Computer languages decide to remove functions sometimes, and will display “depreciation warnings” meaning that function/syntax will not be supported in future releases of the software (MATLAB in this case).
The following sub-sections review a general step process to identify why your code is broken. There are a variety of errors that can occur in MATLAB; however, the following sections will give you a general way of approach to tackle most errors that you encounter.
Read the Error - Typically, error messages contain at least two types of helpful information. As you can see in Figure 1, it provides a line reference where it believes the problem is occuring (this is often the correct line, but not always). The error message also usually contains some helpful information about the problem, and this information has gotten more and more detailed in newer versions of MATLAB.
Isolate the Problem - If you cannot pinpoint the error from an error message or some other information you have gathered, you should try to isolate the problem through other methods. Commenting out (%) irrelevant code to find where the problem begins is a good way to start. Remember, MATLAB reads lines of code sequentially, so you should start at the top and work down or vice versa. You can also try unsuppressing code (remove the semicolon “;” at the end of a line of code) to output the variable value to the command window and to see what value a variable is storing.
Check Syntax and Function Requirements - Most of the time, if you make an error in the syntax of your program, MATLAB will return an error. There is no exact procedure to follow when debugging these errors, but here are a few general steps to try:
In the next lesson, we will learn how to create variables: the first step in writing our own program.