2

For a project, I'm trying to use matlab to call a function in another .m file. However, it says 'Not enough input arguments', even though I do pass what I am fairly certain is enough input arguments.

In eval_square.m:

function f = eval_square(x)

%   fitness function of the magic square
%
%   Parameters
%   ----------
%       x : array, the solution vector that represents a magic square.
%           By default, the solution vector is converted to a magic square
%           columnwisely.
%   Output
%   ----------
%       f : double, the error value of the input solution vector.
%           the mean squared error (MSE) of all each row, column and
%           diagonal sum to the magic constant is computed
%

n = sqrt(length(x));

%More stuff, but error occurs at this line.

in MYNAME_sa.m:

function [xopt, fopt] = MYNAME_sa(dim, eval_budget, fitness_func)

%Stuff

if dim == 2
    len = 12^2;    % length of the solution vector, shoud be 12^2 
                 % when dim == 2
elseif dim == 3
    len = 7^3;    % length of the solution vector, shoud be 7^3 when 
                 % dim == 3
end

%Stuff

s = randperm(len)         
f = fitness_func(s)

%More stuff.

It's supposed to evaluate the random permutation of length 12^2 as a magic square, to see how close it is to optimum (i.e. how close it is to being an actual magic square) and in theory the same for a magic cube (eval_cube), but the same error occurs.

The error in question:

>> MYNAME_sa(2, 10000, eval_square)
Error using eval_square (line 18) 
Not enough input arguments.

Note that line 18 is n = sqrt(length(x));

It doesn't matter if I hardcode eval_square into the function - it seems to understand that I want to call eval_square just fine, but it just doesn't pass s or something? And I don't understand why. I tried hardcoding n to 12 as well, but that doesn't work either, the error pops up when I'm trying to actually use x then. Changing fitness_func to @fitness_func also changes nothing. So my question is, why does this happen and how do I fix it?

3
  • You're giving no input to the function eval_square Commented Dec 4, 2017 at 15:16
  • Don't you need an input here at MYNAME_sa(2, 10000, eval_square) at the eval_square? Because in your definition it says eval_square(x) Commented Dec 4, 2017 at 15:17
  • In continuation to @SardarUsama: What is s while calling the fitness_func? In your code this is unclear. Commented Dec 4, 2017 at 15:20

1 Answer 1

5

Try with

MYNAME_sa(2, 10000, @eval_square)
Sign up to request clarification or add additional context in comments.

1 Comment

To add some extra detail, the third argument needs to be a function handle so that MYNAME_sa can invoke it as needed with a given input.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.