How to call a function in the matlab command window? [duplicate] - matlab

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.

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 occurs:
You are trying to use a function that doesn't exist (yet)
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, that 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 at 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 at 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 unused variables.
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 them 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 variable!?
First off: Chances are, if you're reading this post, then you are better off passing variables as arguments rather than using global variables.
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.

I also got
Undefined function '...' for input arguments of type 'double'.
error and I tried the recommendations mentioned above but they could not solve my problem. Then, I realized that there is a special character (*) in my current working directory so I solve the problem when I changed the name of the directory.
Lastly, do not forget to change the current directory after the change operation by using cd argument.

Another way of looking at the problem:
the input arguments should be in an order such that the explanation of the function can read it.

Related

"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.
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 occurs:
You are trying to use a function that doesn't exist (yet)
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, that 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 at 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 at 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 unused variables.
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 them 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 variable!?
First off: Chances are, if you're reading this post, then you are better off passing variables as arguments rather than using global variables.
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.
I also got
Undefined function '...' for input arguments of type 'double'.
error and I tried the recommendations mentioned above but they could not solve my problem. Then, I realized that there is a special character (*) in my current working directory so I solve the problem when I changed the name of the directory.
Lastly, do not forget to change the current directory after the change operation by using cd argument.
Another way of looking at the problem:
the input arguments should be in an order such that the explanation of the function can read it.

Undefined function in MATLAB

I using the function dtw in the latest MATLAB release, and would like to tweak a few parts of it. To get started, I typed:
edit dtw
I saved the resulting code to file called dtw_Copy.m, and changed the name of the function to dtw_Copy as well. Going through the code line by line with a set of input parameters x and y, around line 90:
[metric, varargin] = getmutexclopt({'absolute','euclidean','squared','symmkl'},'euclidean',varargin);
I receive an error message:
Undefined function 'getmutexclopt' for input arguments of type 'cell'.
I also get this error message if I do not go through the code line by line, and simply type dtw_Current(x,y), after again testing a set of input parameters x and y.
Upon running:
help 'getmutexclopt'
it is indicated that getmutexclopt is not found. I tried also:
edit 'getmutexclopt'
But am told that currentDirectory/getmutexcloptm.m does not exist.
I tried:
which getmutexclopt
And am told that getmutexclopt is not found.
Searching online, I found a resource that seemed straight-forward in trouble-shooting this error. The resource recommends to ensure the toolbox is installed. I am unsure which toolbox supports the function getmutexclopt, and so I type the function name into the website. This results in a message that: "Your search - getmutexclopt - did not match any documents."
The resource also recommends verifying the path used to access the function. I followed the instructions to do so, and when I typed:
which -all getmutexclopt
I receive:
currentDirectory\matlab\toolbox\signal\signal\private\getmutexclopt.m % Private to signal
This seems to indicate that the function is in the signal toolbox, which is private? Is there a possibility to still run dtw_Current(x,y) and/or to run its contents line by line?
Yes, this issue is because the function getmutexclopt is a private function. You'll need to make a copy of that function if you hope to safely call it from your copy of dtw. It appears to be a basic function (type edit private/getmutexclopt.m in your Command Window) so you may be able to add it as a sub-function to your dtw_Copy/dtw_Current.
See also this question – adding private functions to the search path is not allowed.

Call overloaded function from specific toolbox in MATLAB

I have some Matlab-Toolboxes installed. In my Matlab version one of the Toolbox-Functions collides with another. In this case it is hessian. I want to use the hessian function of the symbolic-Toolbox.
When in C/C++ Functions are multiple defined like the function for cos and I still want to use the “standard” cos-function I can write:
std::cos(x);
Is there something similar in matlab?
If you have overloaded methods and want to call the built in one, you can use the function builtin. From the official documentation:
builtin(function,x1,...,xn) executes the built-in function with the
input arguments x1 through xn. Use builtin to execute the original
built-in from within a method that overloads the function. To work
properly, you must never overload builtin.
The syntax for using it is:
[y1,...,yn] = builtin(function,x1,...,xn)
Friendly advice: If you want to try overloading builtin ("hmm, I wonder what would happen"), remember to save stuff first.
In a very similar way that you were describing for c/c++, you can use a specific toolbox function by adding the name of the toolbox first : ToolboxName\function2call()
First use the which command to make sure of which function from which toolbox will be loaded with a specific call syntax.
Since I do not have the toolbox you are mentioning i'll use the classic fopen function as an example.
The first fopen function called without any other parameter will be the built in function which is used to return a handle to a file. Indeed, the which command confirms that:
>> which fopen
built-in (C:\TLAB13a\toolbox\matlab\iofun\fopen)
Now let's say I want to use the fopen function to open a serial port, I need to prefix the call to fopen with the name of the toolbox/object, like so: serial\fopen. Let's first make sure this way of calling point to the right function:
>> which serial\fopen
C:\TLAB13a\toolbox\matlab\iofun\#serial\fopen.m % serial method
Bingo !
And just to make sure this works when you are calling these functions, let's call them for real (with dummy parameters):
>> fopen('toto')
ans =
-1
>> serial\fopen('toto')
Error using serial (line 72)
The PORT must be specified.
It worked. The first simple call to fopen('toto') return -1 because it couldn't find a file named "toto".
The second call to serial\fopen('toto') errored because the serial port was not defined, but the right function was called.
Edit:
You can also override the order in which Matlab will fetch functions by re-ordering the Matlab path. If you put the symbolic toolbox before the other one in your path, then when called without explicit information, Matlab will execute the first function it finds in the path.
I would still recommend the first solution with explicit declaration though, because changing the path order could mess up other function call in case you have many overloaded functions.

Where is stirlerr for binopdf.m?

In matlabs statistical toolbox is the function binopdf which can be modified with edit binopdf. I simply opened the function that way and saved it in my working directory under the name logbinopdf.m.
Without any changes to the code of the function, it now always returns the following error.
??? Undefined function or method 'stirlerr' for input arguments of
type 'double'.
The function stirlerr is used in the code but I can not access it or find any informations about it. Do I have to include something special?
My goal is to modify binpdf.m such that it returns the log of its true value.
Often times if you get the error message that a function is undefined for input of type double, it means Matlab cannot locate the correct function on the path. Try which -all stirlerr to ensure that the function exists. If not, you may just have an issue with your directory structure.
Additionally, stirlerr appears as being "Private to Stats". This suggests that if you remove binopdf from the Stats toolbox directory, you may have trouble calling stirlerr directly. You could try copying it and placing it in your working directory as well.

Undefined function or method for input arguments of type 'double' [duplicate]

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.
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 occurs:
You are trying to use a function that doesn't exist (yet)
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, that 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 at 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 at 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 unused variables.
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 them 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 variable!?
First off: Chances are, if you're reading this post, then you are better off passing variables as arguments rather than using global variables.
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.
I also got
Undefined function '...' for input arguments of type 'double'.
error and I tried the recommendations mentioned above but they could not solve my problem. Then, I realized that there is a special character (*) in my current working directory so I solve the problem when I changed the name of the directory.
Lastly, do not forget to change the current directory after the change operation by using cd argument.
Another way of looking at the problem:
the input arguments should be in an order such that the explanation of the function can read it.