Azzera filtri
Azzera filtri

Short function to find min value in vector

3 visualizzazioni (ultimi 30 giorni)
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!

Risposta accettata

José-Luis
José-Luis il 15 Gen 2013
Modificato: José-Luis il 15 Gen 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 Commenti
MiauMiau
MiauMiau il 15 Gen 2013
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 il 15 Gen 2013
Modificato: José-Luis il 15 Gen 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.

Accedi per commentare.

Più risposte (2)

Jan
Jan il 15 Gen 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 Commenti
José-Luis
José-Luis il 15 Gen 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
Jan il 15 Gen 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.

Accedi per commentare.


Image Analyst
Image Analyst il 15 Gen 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.

Categorie

Scopri di più su Parallel Computing Fundamentals in Help Center e File Exchange

Tag

Community Treasure Hunt

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

Start Hunting!

Translated by