One of the most common errors you’ll encounter we get when programming in MATLAB is the error “Undefined function or variable.” In this article, you’ll learn:

• What the most common cause of the error is
• How to solve it
• What the potential other causes could be

## The Most Common Error: Your Function is Not in Your Search Path

### The Root Cause of the Error

The root of the issue is that MATLAB only sees the functions or .m files contained in the “search path.”

• Semi-transparent folder: You can determine if a folder is in the search path by looking at whether it’s semi-transparent:
As the name of the folders indicates, “folder in MATLAB search path” is in the MATLAB search path, and “folder out of MATLAB search path” isn’t.
• Which: You can use the following MATLAB command to see if MATLAB can find your function:
which functionName -all % display paths related to "functionName"

If MATLAB finds your function, it will display the path of the folder that contains your function. Otherwise, it will display “‘functionName’ not found.”

• Pwd: Because MATLAB always sees the folder you are in (but not the folders inside your current folder), you can know what it is by using the following MATLAB command:
pwd % show you current folder location

If your function is outside of this folder (or in a folder inside your current folder), MATLAB won’t see it unless you add it to your search path, and you’ll get the error “Undefined function or variable ‘functionName’.”

• Path: Finally, you can also see all paths in your search path using the MATLAB command:
path % display every paht in your current MATLAB search path

There are 2 solutions to add a folder (containing your function) that is outside of your current folder:

1. Programmatic solution: Use the MATLAB command addpath:
addpath('path/to/your/folder');
2. Manual solution:
1. Right click on the folder that you want to add to your search path.
2. Click on “Selected Folders.”
You can also click on “Selected Folders And Subfolders” if you want to add the subfolders or use the MATLAB command:

addpath(genpath('path/to/your/folder'));

## Other Potential Causes of the Error

### The Variable is Not Defined

Here are a few suggestions on what to look for. Make sure that:

1. You defined the variable.
2. You didn’t call your variable after using a “clear all” or in another subfunction than the one where you defined it.
3. The definition of your variable isn’t predicated on a condition that hasn’t been met.
4. You are calling the right functions that defined your variable.
5. There are no typos in your function or variable call.

Once, I had both a .p file and a .m file with the same name in my search path.

In that case, MATLAB will take the .p file, not the .m file; if you only defined the variable in the .m file, then you’ll get the error.

### The Function is Local to Another .m File

Keep in mind that you can only use the subfunctions in the file that you defined your main function. (which has the same name as the .m file).

If you try to use a subfunction defined in a different .m file, you’ll get the error “Undefined function or variable ‘functionName’.”

If you want to use this function, you need to define it in a separate .m file that has the same name.

### The Function is Not Included in Your MATLAB Version

It is also possible that your MATLAB version doesn’t have the corresponding function (because you have an earlier version for example) or that you don’t have the corresponding toolbox.

If you want to know your MATLAB version or the toolbox available, use the following MATLAB command:

ver % display your current MATLAB version

Key takeaways:

1. Make sure the function you are using is in the search path by checking that the folder containing your function doesn’t appear semi-transparent
2. To solve the issue, use the addpath MATLAB command:
addpath('path/to/your/folder');
3. Or add the path manually:
1. Right click on the folder that you want to add to your search path.
2. Click on “Selected Folders.”
4. If the error remains, make sure that:
1. There is no typo in your function or variable call.
2. The variable is defined.
3. The function is not local to another .m file.
4. The function is included in your MATLAB version.

Usually, these errors are also due to a misunderstanding of the way functions work in MATLAB. If you feel that’s the case for you, have a look at  MATLAB Functions: A Beginner’s Guide