Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"Undefined function 'function_name' for input arguments of type 'double'."

A question that pops up quite frequently in different shapes and sizes is: Why do I get the following error message:

"Undefined function 'function_name' for input arguments of type 'double'."

This post attempts to address all the different scenarios where this error message can occur, and propose solutions for how it can be resolved.

like image 898
Stewie Griffin Avatar asked Jul 07 '16 08:07

Stewie Griffin


1 Answers

If you stumble upon this error message and don't know what it means. Take comfort in this: 90% of us have googled the same phrase.

"Undefined function 'int' for input arguments of type 'double'."

The error message is pretty self-explanatory, but may still cause confusion. (I chose 'int' at random, it could just as well be 'train', 'table', 'my_function' or anything else).

There are two main cases where this error occur:

  1. You are trying to use a function that doesn't exist (yet)
  2. You are trying to access an element in a variable that doesn't exist (yet)

What do you do if you get this error?

First you might want to try which. This will tell you whether or not the function or variable you're trying to use is defined.

which int 'int' not found. 

It's quite obvious, Matlab can't find any functions or variables named int. Trying to use it is therefore futile. Let's compare it to max:

which max built-in (C:\Program Files\MATLAB\R2014b\toolbox\matlab\datafun\@logical\max)  % logical method 

But, you might get the following, even if you get the "Undefined function 'x' ...". If so, see point 3 below.

which x x is a variable. 

1. But the function "int" exists! It is even documented here!?

Yes, int exists, but only if you have the Symbolic Toolbox. Since Toolboxes are additional packages that must be purchased separately (and can be quite expensive), chances are you don't have that package.

If you get the "Undefined function" error, but find the function in the documentation, have a look in the menu to the left, or simply check the address. Standard Matlab functions have addresses such as:

mathworks.com/help/matlab/ref/max.html                    ^^^^^^ 

Notice the "matlab" part. If you see this, then you are using a function that is part of the core Matlab.

If however, you see an address such as the one below, then the function you are trying to use is part of the Symbolic Toolbox:

mathworks.com/help/symbolic/int.html                    ^^^^^^^^ 

or maybe it's part of the Neural Network Toolbox:

mathworks.com/help/nnet/ref/train.html                    ^^^^  

Solution: Find another function that isn't part of a toolbox you don't have. Chances are you'll find what you are looking for if you are a skilled googler. If you can't find it, ask for help. Explain what you have tried, and show that you have done some effort!

2. But the function is documented here, and is part of core Matlab!?

Even though a function is part of the standard Matlab installation, and is well documented, you may still get this error. The most likely cause for this error is that you are using an older version of Matlab. If you check the documentation you'll see the following in the bottom of the page:

Introduced in R2013b

So, if you are using R2012b then you can't use for instance table.

Solution: Check if the function is defined in your version of Matlab. If it's not yet implemented then you either need to update Matlab or find another way to do it. An alternative to table can for instance be to use cells or structs.

3. But the variable "my_variable" exists! I created it in the line above!

No, you didn't. Chances are you created the variable myvariable, my_Variable, my_Variable or something similar in the line above. It's also possible that you have created it, but have accidentally cleared it.

Solution: Go through the code. Look for typos, places where you have accidentally cleared the variable etc. Inside the Matlab editor you will get the following line in the bottom if you mark a variable: "3 usages of "x" found" if you have defined and used the function. You will also get an orange line underneath variables that are unused.

4. But I get "y is a variable" when I type which y?

If you get the error above "Undefined function 'y', but which tells you y exists, your error message contains a few more lines:

my_function(x) Undefined function or variable 'y'. Error in my_function (line 2) t = x*y;   >> which y y is a variable. 

What this tells you is that you have a variable called y in your Matlab Workspace (also check this link).

The problem is that functions can't access this workspace:

Functions do not use the base workspace. Every function has its own function workspace.

If you want a function to see and use a variable, you must pass it as an argument. This way the variable will be part of the local workspace for that function. Similarly, if you want variables created inside the function to be accessible outside of the function you must have it as output from the function.

Solution: Pass the variables you want to use as input arguments to the function you use. Make sure the names inside the functions are internally consistent. I.e. it must have the same name throughout the function. Note that there is no connection between the variable names outside and inside the function.

5. But I pass the variable as an input to the function, but I still get the same error message!?

Yes, you probably use the variable as input. However, the variable names are not necessarily the same in different functions (most often they are not).

Suppose you have the function:

function output = my_function(x)   output = 2*y; end 

You'll get the same error as above if you call it from the workspace as in the code below, even though you are using y as input variable, and use y inside the function.

y = 3; res = my_function(y) 

This is because inside the function my_function, the variable you use as input will be called x, regardless of what it was called outside the function.

Solution: Change the name of the input variable name in the function header, or change the name of the variable throughout the function.

6. But I have created x as a global varible!?

First off: I recommend you to not use global variables! It's much better to pass variables as arguments.

It's not enough to declare a variable as global in the Matlab workspace. It must be declared in every function you use it in. So, if you have a global variable x, you need to do global x in every function.

Solution: Rewrite your code and pass variables as arguments instead of using global variables. If this is not an option, add global x in all functions where you're using it.


In addition to this answer, you can refer to the official Matlab FAQ.

like image 67
Stewie Griffin Avatar answered Oct 06 '22 02:10

Stewie Griffin