What are the different types of errors that can be introduced while programming in MATLAB and how should I go about debugging MATLAB programs?

52 views (last 30 days)
I would like some information on the different types of errors that can be introduced while programming in MATLAB. I would also like to know how I can go about debugging my MATLAB programs when such errors are introduced.

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 14 Oct 2022
Edited: MathWorks Support Team on 14 Oct 2022
There are two main types of errors that can be introduced while programming in any language:
1. Syntax Errors
2. Algorithmic Errors
Syntax Errors:
A syntax error occurs when the calling syntax you use for a function is incorrect, or when you provide the function with inputs that are of the wrong shape, size, and/or type, or are otherwise not valid for the function in question. A typo, or typographic error, could be considered as a syntax error. If it occurs while you are typing a function name, it can be easy to find. For example, typing
cod(pi)
when you meant to type
cos(pi)
will return an error message that is easy to understand:
ERROR: ??? Undefined function or variable 'cod'.
However, if you mistype a variable name, it can lead to unexpected results that can be extremely difficult to track down and eliminate. For example, if you type
x=cos(y);
when you really meant to type
x = cos(t)
and "t" is of different size than "y", you will likely receive an error message about the size of the array when you try to manipulate it later in your code, long after the assignment statement has been executed.
Algorithmic Errors:
An algorithmic error occurs when the program executes perfectly, but the result you receive is not what you expect. For instance, if you wrote a program to add two numbers, passed it 2 and 3, and received 6 as a result (with no error or warning messages) that would be an algorithmic error.
The main technique involved in debugging these types of bugs is to compute the answer to the problem you expect by some means other than MATLAB (or by using an example worked in a text on the subject) and stepping through the code. You can do so with the help of breakpoints, which are used to pause a program while it is executing. When the program is paused at a breakpoint, it is possible to check the state of the program where the breakpoint is set. By making sure whether the program computes the correct results at each step, one can eventually locate the error precisely.
For more information on debugging MATLAB files, please refer to the following documentation.

More Answers (0)

Categories

Find more on Programming in Help Center and File Exchange

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!