MATLAB error: Undefined function or method X for input arguments of type 'double' [duplicate] - function

This question already has answers here:
"Undefined function 'function_name' for input arguments of type 'double'."
(3 answers)
Closed 5 years ago.
I'm a new user of Matlab, can you please help:
I have the following code in an .M file:
function f = divrat(w, C)
S=sqrt(diag(diag(C)));
s=diag(S);
f=sqrt(w'*C*w)/(w'*s);
I have stored this file (divrat.M) in the normal Matlab path, and therefore I'm assuming that Matlab will read the function when it's starting and that this function therefore should be available to use.
However, when I type
>> divrat(w, C)
I get the following error
??? Undefined function or method 'divrat' for input arguments of type 'double'.
What is the error message telling me to do, I can't see any error in the code or the function call?

You get this error when the function isn't on the MATLAB path or in pwd.
First, make sure that you are able to find the function using:
>> which divrat
c:\work\divrat\divrat.m
If it returns:
>> which divrat
'divrat' not found.
It is not on the MATLAB path or in PWD.
Second, make sure that the directory that contains divrat is on the MATLAB path using the PATH command. It may be that a directory that you thought was on the path isn't actually on the path.
Finally, make sure you aren't using a "private" directory. If divrat is in a directory named private, it will be accessible by functions in the parent directory, but not from the MATLAB command line:
>> foo
ans =
1
>> divrat(1,1)
??? Undefined function or method 'divrat' for input arguments of type 'double'.
>> which -all divrat
c:\work\divrat\private\divrat.m % Private to divrat

As others have pointed out, this is very probably a problem with the path of the function file not being in Matlab's 'path'.
One easy way to verify this is to open your function in the Editor and press the F5 key. This would make the Editor try to run the file, and in case the file is not in path, it will prompt you with a message box. Choose Add to Path in that, and you must be fine to go.
One side note: at the end of the above process, Matlab command window will give an error saying arguments missing: obviously, we didn't provide any arguments when we tried to run from the editor. But from now on you can use the function from the command line giving the correct arguments.

The most common cause of this problem is that Matlab cannot find the file on it's search path. Basically, Matlab looks for files in:
The current directory (pwd);
Directly in a directory on the path (to see the path, type path at the command line)
In a directory named #(whatever the class of the first argument is) that is in any directory above.
As someone else suggested, you can use the command which, but that is often unhelpful in this case - it tells you Matlab can't find the file, which you knew already.
So the first thing to do is make sure the file is locatable on the path.
Next thing to do is make sure that the file that matlab is finding (use which) requires the same type as the first argument you are actually passing. I.el, If w is supposed to be different class, and there is a divrat function there, but w is actually empty, [], so matlab is looking for Double/divrat, when there is only a #(yourclass)/divrat. This is just speculation on my part, but this often bites me.

The function itself is valid matlab-code. The problem must be something else.
Try calling the function from within the directory it is located or add that directory to your searchpath using addpath('pathname').

The error code indicates the function definition cannot be found. Make sure you're calling the function from the same workspace as the divrat.m file is stored. And make sure divrat function is not a subfunction, it should be first function declaration in the file. You can also try to call the function from the same divrat.m file in order to see if the problem is with workspace selection or the function.
By the way, why didn't you simply say
s = sqrt(diag(C));
Wouldn't it be the same?

Also, name it divrat.m, not divrat.M. This shouldn't matter on most OSes, but who knows...
You can also test whether matlab can find a function by using the which command, i.e.
which divrat

I am pretty sure that the reason why this problem happened is because of the license of the toolbox (package) in which this function belongs in. Write which divrat and see what will be the result. If it returns path of the function and the comment Has no license available, then the problem is related to the license. That means, license of the package is not set correctly. Mostly it happens if the package (toolbox) of this function is added later, i.e., after installation of the original matlab. Please check and solve the license issue, then it will work fine.

Related

Tcl/Tk: "bad window path name" when it is not a window path

I have scripts which were used without Tk. When I added Tk one line is treated as path to window and I don't understand why.
Error infor is like:
bad window path name "* Log directory: D:/tests/log/2016_10_13_17_30_29"
It is when this line is executed:
message "* Log directory: ${dirname}"
dirname is resolved as path to directory, for example:
D:/tests/log/2016_10_13_17_30_29
message is a simple procedure for logging to file:
proc message {text} {
global test_log_filename
puts -nonewline $test_log_filename "[timestamp] "
puts $test_log_filename $text
}
When I do not use Tk it is working correctly. As I understand it takes message procedure argument as path to Tk window, but I don't know why. Of course such a window doesn't exist.
I also tried to invoke something like message test to confirm that it is argument problem. It gives error bad window path name "test"
Could you please help with it?
Tk also defines a command called message (it's a widget similar to a label, but with a somewhat different word-wrapping algorithm; it's not recommended for new code) and for some reason, that's winning out. While you could probably force yours to be used instead by directly sourceing your code after the package require Tk, it's unlikely to be a good idea as you may find some other code breaks because of it. (I can't remember if it is currently used in the standard dialogs, but that's the sort of thing which might go wrong.)
The easiest thing is to change the name of your logging procedure. Perhaps to Message (with the capital letter). That shouldn't take long as it's effectively not much more than a global search-and-replace.

IDL undefined procedure

I'm using IDL 8.3 on Mac 10.9.3
I am running a script that calls in a procedure. The procedure I am calling in is contained in a directory that I included in IDL's path (I did this by going under IDL->preferences->IDL->paths and adding the directory). However, when I attempt to run the script, I get the error message: "% Attempt to call undefined procedure/function: 'procedure.pro'. % Execution halted at: $MAIN$". The weird thing is is that it still lists all the syntax errors in the procedure that is supposedly 'undefined'. Also, when I type the procedure.pro name into the IDL prompt, it lights up teal/blue color (meaning it recognizes the procedure).
I tried making a very simple simple.pro, put it into the same directory I mentioned before, typed it into the IDL prompt (it turned teal/blue), and it ran perfectly with no errors.
I am unsure why the procedure.pro file is 'undefined' since it is contained it its path, and I proved with simple.pro that .pro files in this path will run correctly.
A couple of things to check:
Is the routine called as a procedure and defined as a procedure (or called/defined as a function)?
Does the name of the file match the name of the routine?
well, the procedure I was attempting to call in contained other procedures/functions that weren't included in IDL's original library. I just had to download these separate procedures/functions, and the syntax errors went away, along with the 'unidentified procedure' error message.

Erlang: calling rr(?MODULE) from beam executable?

I'm not entirely sure how to define an Erlang function within an Erlang module. I'm getting the following error:
11> invoke_record:invoke().
** exception error: undefined function erlang:rr/1
From this simple code trying to invoke the rr(?MODULE). from within the beam executable in order to "initialize" records so that it doesn't need to be called from the shell every time.
-module(invoke_record).
-export([invoke/0]).
-record(process, {pid,
reference="",
lifetime=0
}).
invoke() ->
erlang:rr(?MODULE).
The command rr("file.hrl"). is meant to be be used only in shell for debugging purposes.
As other users highlighted in their answers, the correct way to import a record (or a function) contained in a .hrl file within your erlang code consists in using the command -include("file.hrl').
Once you have included the .hrl file in your code (and usually in a module based on OTP behaviours this is done after the -export(...) part) you can refer to the Erlang record (or function) without any problem.
rr is a shell command. You cannot use it it compiled code.
http://www.erlang.org/doc/man/shell.html
If your intent is to read many record definitions in the shell, in order to facilitate the debug, you can write a file containing all needed include statements and simply invoke rr once in the shell.
in rec.hrl:
-include("include/bank.hrl").
-include("include/reply.hrl").
and in the in the shell
1> rr("rec.hrl").
[account,reply]
2>
I didn't find any way to execute this automatically, when starting the VM.
When working on a project, you can gather all necessary includes and other command line arguments that you want to use for that particular project in a plain text file. After having made the plain text file, you can start your shell:
erl -args_file FileName
where FileName is the name of the plain text file. Note that all command line arguments accepted by erl are allowed. See also erl Flags in the ERTS Reference Manual

function defined in .zshrc not found when called from a script

I have defined some variables, aliases and functions in my .zshrc file:
export MY_VAR="example"
alias my_alias="echo an example"
function say_hello
{
echo "say hello"
}
I have verified that all three are defined and whatnot when called from the terminal. However, when I try to call the function say_hello from another script (which itself is called from the same terminal), the function does not appear to be defined. I see a 'command not found' error. The alias and variable MY_VAR appear to be defined just fine when referenced from this other script.
Any ideas on what might be going on? Thanks.
When zsh is called from a terminal it is invoked in interactive mode that causes zsh to source additional configuration files, including $ZDOTDIR/.zshrc ($HOME/.zshrc). By default (in non-interactive mode in non-login shell) it sources only /etc/zsh/zshenv* and $ZDOTDIR/.zshenv ($ZDOTDIR is most of time $HOME) files, see last but two section of man zsh named STARTUP/SHUTDOWN FILES. I have no idea why alias is defined (how exactly did you check?), but MY_VAR is defined in script because you exported it.
* /etc/zshenv according to doc, much likely they are Gentoo maintainers of zsh package who changed it to /etc/zsh/zshenv.
Note: it is bad idea to put such functions into .zshenv file as it creates implicit dependency. You should use script libraries instead: put it into
~/.zsh/say_hello.zsh
and do
source ${ZDOTDIR-$HOME}/.zsh/say_hello.zsh
in both your script and .zshrc.

Why can't Matlab see my function?

My function is definitely working; it's tested and was at one point being recognized.
Here's the function prototype:
function [X Y] = calculateEllipse(x, y, a, b, angle)
%# Code here
end
Here's the call I'm making from the Matlab terminal:
calculateEllipse (612, 391, 107, 60, 331)
Here's the error popping out at me:
??? Undefined function or method 'calculateEllipse' for input arguments of
type 'double'.
Now, I am 100% positive I am in the same directory as the function. I even used
addpath('C:\path-to-function')
to make sure. It's just not working, and I'm baffled.
Any help is appreciated.
To summarise other posts, here is a workflow for determining the cause of the problem.
You mistyped the name of the function. Check the function definition and make sure it really it called calculateEllipse.
You saved the function to a file named something other than the function name. Check the filename of the function and make sure it matches the function name.
The folder containing the function name isn't on the MATLAB path. There are several ways to check this. Type path to see the current path, or which calculateEllipse to find the location that MATLAB is using for that file. (If there is a problem, that last command will display 'calculateEllipse' not found.. Note that addpath does not permanently update the path, so when you close down MATLAB, the path will be reset. Use savepath for this.
The folder containing the function is a subdirectory of matlabroot. These folders are reserved for fully fledged toolboxes; bad things happen when you store your code here. See Bob's answer for more information.
Other useful things to check are:
Can you call other functions that are stored in the same folder?
If you save the function in a different folder, will it run then?
Adding to what Jeff said; another possibility is that you placed the function somewhere inside of your MATLAB installation. By default MATLAB does't re-search its own file structure for new files; it assumes that its internal file structure remains unchanging. Make sure that you're saving the file (which, as Jeff pointed out, must be named calculateEllipse.m) somewhere outside of your MATLAB installation.
See https://www.mathworks.com/help/matlab/matlab_env/toolbox-path-caching-in-the-matlab-program.html, or go to the MathWorks web site and search for
path cache
for more information.
The key to this problem is this: %Has no license available. This implies that a function in the directory of the function you are trying to use has the same name as a function in a toolbox you do not own. MATLAB by default disables the whole directory and not just the function of the same name in a toolbox you do not own. Here is an example:
files in directory:
myfunction.m
scoobydoo.m
blackman.m
If I do not own the "Signal processing toolbox," then blackman.m will disable the whole directory.
I can think of a couple of reasons this could happen.
First, as Jeff said, you could have named the file 'calcEllipse.m' instead of 'calculateEllipse.m'. In which case you need to rename the function to be the same as the m file you saved.
Second, you have not added the correct path. There is no reason for this to give an error to my knowledge otherwise. Double check that you have added the path to the m file that is being saved. An easy way to check is if you type in 'calculateEll' and then press tab, does the autocomplete work? If not you are out of the path.
Hope it is one of those thing you can quickly fix!