Short function to find min value in vector

3 次查看(过去 30 天)
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!

采纳的回答

José-Luis
José-Luis 2013-1-15
编辑:José-Luis 2013-1-15
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 个评论
MiauMiau
MiauMiau 2013-1-15
I actually do pass a vector, see above.
A = [2 4 1]
now I have also renamed my function to findmin. So I call the function with
findmin(A)
nevertheless, I get the error message "Undefined function 'findmin' for input arguments of type 'double'"
What does that mean?
Thanks
José-Luis
José-Luis 2013-1-15
编辑:José-Luis 2013-1-15
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.

请先登录,再进行评论。

更多回答(2 个)

Jan
Jan 2013-1-15
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 个评论
José-Luis
José-Luis 2013-1-15
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
Jan 2013-1-15
@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
Image Analyst 2013-1-15
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.

类别

Help CenterFile Exchange 中查找有关 Parallel Computing Fundamentals 的更多信息

标签

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by