MATLAB奇怪的“输入参数过多"错误 [英] MATLAB odd 'Too many input arguments' error
问题描述
对于一个项目,我正在尝试使用matlab在另一个.m文件中调用一个函数.但是,即使我确实通过了相当确定的输入参数,它也会显示输入参数不足".
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.
在eval_square.m中:
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.
在MYNAME_sa.m:
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.
应该将长度为12 ^ 2的随机排列评估为一个魔方,以了解它与最佳距离有多近(即,与实际的魔方有多近),并且在理论上与魔方相同(eval_cube),但会发生相同的错误.
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.
有问题的错误
>> 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));
是否将eval_square硬编码到函数中并不重要-似乎理解到我想很好地调用eval_square,但是它没有传递s或其他信息?而且我不明白为什么.我也尝试将n硬编码为12,但这也不起作用,然后我尝试实际使用x时会弹出错误.将Fitness_func更改为@fitness_func也不会更改任何内容. 所以我的问题是,为什么会发生这种情况以及如何解决?
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?
推荐答案
尝试
MYNAME_sa(2, 10000, @eval_square)
这篇关于MATLAB奇怪的“输入参数过多"错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!