필터 지우기
필터 지우기

Short function to find min value in vector

조회 수: 3 (최근 30일)
MiauMiau
MiauMiau 2013년 1월 15일
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