Short function to find min value in vector
2 views (last 30 days)
Show older comments
Hi
I have written this simple function:
function minv = findm(vec)
minv= vec(1);
for i = 2:length(vec)
if vec(i) <= minv
minv = vec(i)
end
end
and I feed it with A = [2 4 1] with:
s = findm(A)
But I get the error message "Not enough input arguments". Why is that? I don't see any obvious mistake in the code..
Thanks!
0 Comments
Accepted Answer
José-Luis
on 15 Jan 2013
Edited: José-Luis
on 15 Jan 2013
findm() is a built-in Matlab function. Rename your function to something else, e.g. findmin(). Even if you run your function from the folder it is saved in, it is always a good idea to avoid naming your custom functions as Matlab's built-in function. Alternatively, you could use the built-in min() function.
2 Comments
José-Luis
on 15 Jan 2013
Edited: José-Luis
on 15 Jan 2013
That means that findmin() is not on the path or the current directory, and Matlab can't find it. Try to run the function from the folder it is located in. Alternatively, as Jan says, add the folder where the function is to the path. Then you will be able to run it from everywhere.
More Answers (2)
Jan
on 15 Jan 2013
The M-file must be called "findmin.m", while the actual name of the function does not matter. This M-file must be either in the current directory (see cd command) or better in a user-defined folder in Matlab's path:
addpath('D:\MyMatlabFiles\Experiments')
3 Comments
José-Luis
on 15 Jan 2013
It means that you are trying to pass a double (use wikipedia for "double precision") to a function, but that there is no function called findmin that accepts double.
I agree that error messages can be cryptic, but I don't think this one is. It is just a matter of getting used to the terminology.
Please accept an answer if it helped you.
Jan
on 15 Jan 2013
@MiauMiau: You can define a function in a subfolder like \@cell\XYZ.m or \@double\ABC.m . Functions inside such a folder are called, when the input has the corresponding type (with some further rules if the input has multiple inputs). Now calling XYZ(1) or ABC({1,2,3}) must fail, because the corresponding functions are not defined for this specific class of inputs. The error message "Undefined function XYZ" would be wrong and the exact message is "Undefined function XYZ for input argument of type DOUBLE". You would get more information, when you type: which XYZ -all, namely a list of all functions called XYZ together with the path, such that you can see that it is defined inside a @cell folder.
Image Analyst
on 15 Jan 2013
When you call it you have to pass in a vector. You didn't. Let's see the line where you actually called findm(). And tell me whether it was from the command window or another function or script.
0 Comments
See Also
Categories
Find more on Parallel Computing Fundamentals in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!