How to Recover from Mistakes in MATLAB

By Jim Sizemore, John Paul Mueller

Everyone makes mistakes, even with MATLAB. You might think that experts don’t make mistakes, but any expert who says so definitely isn’t an expert. Making mistakes is part of the learning process. It’s also part of the discovery process. If you want to do anything important with MATLAB, you’re going to make mistakes. You need to understand what to do when mistakes happen.

Understanding the MATLAB error messages

MATLAB tries to be helpful when you make mistakes. It doesn’t always succeed, and you may not always understand the message, but it does try. In most cases, you see an error message that provides enough information for you to at least get started in finding the mistake. For example, if you try to use the clc command but type it in uppercase, you get

Undefined function or variable ‘CLC’.

The error message is enough to get you looking for a solution to the problem, even when the problem isn’t completely clear. In some cases, MATLAB even provides the correct command for you. All you have to do is press Enter and it executes.

Some errors are a little harder to figure out than others. For example, this is what happens when you try to use idivide() without specifying that the inputs are integers.

image0.jpg

In this case, you can ignore the links and what looks like gobbledygook. Focus on the second line. It tells you that one of the arguments must belong to the integer class. (Remember that the default is to assume that all numbers are doubles.)

It’s really saying that you need integer values as input to idivide(). When you get past the odd bits of information, you can more easily figure out how to fix the problem.

Stopping MATLAB when it hangs

Most of the time, MATLAB is extremely forgiving. You can make absolutely horrid mistakes, and MATLAB simply provides what it considers a helpful message without destroying anything. However, at times MATLAB has to chew on a bit of code for a while before it discovers the error, such as when you’re working with a really large array.

You can tell that MATLAB is working because the status bar shows Busy rather than Ready. In this case, you can talk to your buddy in the next cubicle, get a cup of coffee and read a good book, or press Ctrl+C to stop MATLAB from going any further.

Pressing Ctrl+C always stops MATLAB from performing any additional processing. The status bar indicates Ready as soon as the processing is completely stopped. Don’t use this option unless you really need to do so because MATLAB truly does stop right in the middle of what it’s doing, which means that whatever you were doing is in an uncertain state. It’s good to know that the option exists, though.