views:

4698

answers:

6

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 Im assuming that Matlab will read the function when its starting and that this function therefore should be available to use.

divrat(w, C) ??? Undefined function or method 'divrat' for input arguments of type 'double'. What is the error message telling me to do, I cant see any error in the code or the function call?

+1  A: 

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?

hakan
A: 

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'.

Mr Fooz
A: 

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').

BastiBechtold
A: 

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.

sundar
A: 
Marc
A: 

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

Todd