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.
Related
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.
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.
I am having a problem with the findpeaks function, this function is in the signal processing toolbox and also the program has another version of it (user defined function). I need to call the on in the signal processing toolbox not the user defined one, also I can't rename the user defined function for many reasons. Can anyone help me in calling the toolbox function.
The precedence order used by MATLAB is described in their help pages. It states that functions in the current folder (9.) are preferred over functions elsewhere in the path (10.). Then, the first appearance of the function in the path is chosen. This allows for a number of possible solutions:
1. cd to folder
A very simple method is simply to change the current workspace directory to the folder of the function you need to call, i.e. cd either to the place where your user-defined function is, or cd to the toolbox path. Note: This is rather inelegant, but probably sometimes the simplest solution.
2. Reorder path
As mentioned, MATLAB choses the first occurence of the function in the path. You can thus re-sort the path variable, so the folder where your user-defined function is, appears last. The path variable can be viewed and manipulated using the path function. Note: Then you can only call the toolbox function. Otherwise you'd have to resort the path again.
3. Function handles
If you need to be able to call both functions, it can be useful to create a function handle for both versions. For that, you have to cd into the folders where the functions are defined and create a new handle there:
cd('path/to/userdefined/function')
userFindPeaks = #findpeaks;
cd('path/to/MATLAB/installation/toolbox/signal/signal')
toolboxFindPeaks = #findpeaks;
You can then call the functions using feval.
Of course, as Adriaan mentions in the comments, it is best not to use the names of already defined functions for your own functions or for variable names.
I just came here looking for the same thing... I ended up using builtin.
https://uk.mathworks.com/help/matlab/ref/builtin.html
[y1,...,yn] = builtin(function,x1,...,xn)
#arr_sea actually posted a link in one of the folded comments which uses this function in a different context.
gpuArray is a function used to create array on GPU in matlab. Here I don't always want to use the gpuArray, so I write a function called gpuArray (below) to return a normal matrix when necessary, so I don't need to change the code much. I have already added this function to the path. But when I call the gpuArray function, it still return a gpuArray.....does anyone know why....thanks a lot!
function A = gpuArray(A)
This is....not the best idea. Having multiple functions with the same name will end up leading to quite a bit of ambiguity that a search + replace on your code to an alternate function with a unique name won't cause. But if you insist on doing this, then you need to be conscious of how MATLAB searches for functions. The order is (from http://au.mathworks.com/help/matlab/matlab_prog/function-precedence-order.html):
Imported package functions
Nested functions within the current function
Local functions within the current file
Private functions
Object functions
Class constructors in # folders
Functions in the current folder
Functions elsewhere on the path, in order of appearance
So to make sure your function takes precedence over the built-in function it needs to be higher on that list. You can include your function as a sub-function in the current file (#2 or #3), a private function (#4), create a class and use those functions (#5 and 6), put your function in the same folder as the code invoking it (#7) or ensure that your function is in a folder higher up in the search path than the built-in function (#8). I suspect that your \Documents\MATLAB folder or whichever your gpuArray function is in is actually lower in your folder path than the built-in function so #8 above fails. You can move the place of that folder in your search path or, a better idea, change the name of your function to something unique and change the code that calls it.
You should always be able to type "which gpuArray" to find out which "gpuArray" that Matlab will invoke. I'm assuming that it will not point to yours.
To try to get Matlab to use your gpuArray, you should try adding the path to your function to the Matlab path. Try something like:
%add the path to *my* gpuArray function
addpath('C:\MyDirectory\SomeOtherDirectory\MyMfiles\');
Good luck!
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.