1

Here is the code of the functions I'm using.

function max  = find_max( matrix )

a = -1;
for i = 1:numel( matrix ),
    if ( matrix(i) > a),
        a = matrix(i);
    end
end

max = a;

end



function maxs = find_maxs( matrix, count )

maxs = [];
while (count > 0),

    a = -1;
    for i = 1:numel( matrix ),
        if ( matrix(i) > a && ~is_present(maxs, matrix(i))),
            a = matrix(i);
        end
    end

    maxs = [maxs a];
    count = count - 1;
end

end


function present = is_present( vector, element )

for i = 1:numel( vector ),
    if ( vector(i) == element),
       present = TRUE;
       return;
    end
end

end

When I try to call:

m = [1 2 3 4];
disp(is_present(m, 1));

Or the function find_maxs, I get this error.

??? Undefined function or method 'is_present' for input arguments of type 'double'.

I'm new to matlab and I don't understand why I'm getting this error. The name of the file is find_max.m, the same name of the first function (which works fine).

AR89
  • 3,548
  • 7
  • 31
  • 46
  • 8
    MATLAB requires that you create a separate .m file for every function you write. The only exception is "helper functions" which are only called by the function who owns the .m file. – eigenchris May 07 '15 at 16:19
  • 2
    additional to @eigenchris comment: everything you need to know about that topic you can find [here](http://stackoverflow.com/questions/3569933/is-it-possible-to-define-more-than-one-function-per-file-in-matlab) – Robert Seifert May 07 '15 at 16:35

1 Answers1

1

Just to expand on eigenchris's comment (I'd put it as a comment but I don't have the privilege yet),

Each function should have it's own m file, and the m file should have the same name as the function.

Ex:

function max = find_max( matrix )

should be in a file named 'find_max.m'

ErinGoBragh
  • 336
  • 4
  • 20