Error: Undefined function <func_name> for input arguments of type 'double'.

3 vues (au cours des 30 derniers jours)
Atinesh Singh
Atinesh Singh le 10 Fév 2017
Commenté : Stephen23 le 10 Fév 2017
Below is a small part of the big code, Where I'm getting an error at line 3. Undefined function 'f1' for input arguments of type 'double'.
Below I've changed the code little bit as compared to the source to make it more readable.
function fit = dummy(x)
fhd = str2func('f1');
fit = feval(fhd, x); % Error Here
%------------------------------------------------------------------------------
% Elliptic Function
%------------------------------------------------------------------------------
function fit = elliptic(x)
%TODO Do we need symmetry breaking?
%TODO Implement to support a matrix as input.
[D ps] = size(x);
condition = 1e+6;
coefficients = condition .^ linspace(0, 1, D);
fit = coefficients * T_irreg(x).^2;
end
function fit = f1(x)
persistent xopt lb ub
[D ps] = size(x);
load 'datafiles/f01.mat';
idx = checkBounds(x, lb, ub);
x = x-repmat(xopt, 1, ps);
fit = elliptic(x);
fit(idx) = NaN;
if ~isempty(idx)
warning "Some of the solutions are violating boundary constraints.";
end
end
end
Please help me to sort out the problem. For the complete code checkout "benchmark_func.m" in the "matlab.zip" attached.

Réponse acceptée

Guillaume
Guillaume le 10 Fév 2017
I suspect the problem is str2func not seeing the local function. That's the problem with using strings to store code, the JIT compiler doesn't always work properly.
Any reason you're using an outdated method of creating and using function handles?
fhd = @f1;
fit = fhd(x);
would work, is simpler and possibly faster as well.
If f1 is one of many possibilities to be chosen by that dummy function:
fchoices = {@f1, @f2, @f3};
fhd = fchoices{choice};
fit = fhd(x);
  1 commentaire
Stephen23
Stephen23 le 10 Fév 2017
+1 for recommending function handles (much better than awful strings).

Connectez-vous pour commenter.

Plus de réponses (1)

Massimo Zanetti
Massimo Zanetti le 10 Fév 2017
Modifié(e) : Massimo Zanetti le 10 Fév 2017
Refer to your 2nd line of code
fhd = str2func('f1');
The string 'f1' evaluates to nothing in Matlab, therefore the command
fit = feval(fhd, x);
fails. To let it work, 'f1' must be an anounimous function definition. Read str2func help page.

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by