How to find the problem in your code

A guide to debugging your own code
As we are getting more into more complicated Matlab stuff, we thought we would drop a list of steps we find helpful when we're trying out new code (in Matlab and other places). Try to follow this list when you are practicing---in our experience, we rarely get past item 5 before we realize what we've done wrong.
 
1. Read the instructions very carefully (when independently coding: remember what the goal of the code is)
 
2. Check for typos. Your file names, functions, and variables must be EXACT matches!
 
3. Check again for typos! Common problems:
a. Spaces in all the right places
b. All brackets, parentheses, and string quotes are opened AND closed
c. You've accidentally put an apostrophe in your string and didn't mark it appropriately
 
4. Check for path errors. Common problems:
a. A Matlab function you're trying to use is not on the path;
b. Your path is wrong (particularly if hard-defined in the code. Very hazardous when moving between different computers with different directory names!)
c. You have not entered the correct path based on what your current working directory is. E.g. trying to add a file called worksheet_10.txt that is in a worksheet10 directory, while you are in a worksheet9 directory (would need to enter ../worksheet10/worksheet_10.txt, not just worksheet_10.txt as the file you wish to add)
d. There is a space in your path, and you haven't protected that space (using quotes around the entire path, for example) 
 
5. Examine your error message. Is it informative? Are you banging your head against something because...
a. A variable is undefined? 
- Are you sure there wasn't a typo in the variable, either when you defined it or when you used it? 
- Did you change a variable name and forget to do it all the way through? 
- Did you change where you defined the variable and forgot that you were calling it before then? 
- Did you call a function and not include enough input arguments? 
b. A function doesn't exist?
- Are you sure there isn't a typo?
- Are you sure it's on the path? (Tip: try which FUNCTION NAME to see if it is on the path)
 
6. If that fails, sometimes a Google search with your error text will be helpful. 
 
7. Start writing a message to the greater Slack channel. Include what you're trying to do, what the error is, and what you've done so far to try to fix it. (Frequently, coders get to this point and instantly come up with the solution. Much like how you find typos in your term papers as soon as you submit them!)
 
8. If that fails, send the message. You may find the solution tinkering around while waiting for a response, at which point you can send another message saying you figured it out. But sometimes the bugs run deeper than what you have the capacity to tinker with.


Keywords:
BASIC, coding, errors 
Doc ID:
106231
Owned by:
Robin K. in SMNG Lab Manual
Created:
2020-09-29
Updated:
2025-09-15
Sites:
Speech Motor Neuroscience Group