There are some good suggested solutions to this from Cris Luengo on this parallel stackoverflow post:
Multiple dispatch problem when trying to call the feval function with a LinearModel as an argument.
3 views (last 30 days)
Show older comments
Answered: Bill Tubbs on 24 Feb 2023
I set up a workflow where I am using feval to call functions programmatically and ran into a major stumbling block with one particular function call. I've isolated the problem below and explain what I think is happening.
x = 200;
[y_mean, y_sigma, y_int] = lin_model_predict(model, x, vars, params); % works fine
assert(round(y_mean, 4) == 139.7200);
assert(isequal(round(y_int, 4), [137.5938 141.8462]));
% Test again using feval
f_name = "lin_model_predict"; % this will later be set programatically
[y_mean, y_sigma, y_int] = feval(f_name, model, x, vars, params); % raises error
The error raised is:
Error using classreg.regr/CompactPredictor/feval
Too many output arguments.
Error in test_lin_models (line 46)
[y_mean, y_sigma, y_int] = feval(f_name, model, x, vars, params);
So instead of calling the normal feval function, it seems to be calling the feval method of CompactPredictor class. From the documentation in /Applications/MATLAB_R2021b.app/toolbox/stats/classreg/+classreg/+regr/CompactPredictor.m:
function yPred = feval(model,varargin)
%FEVAL Evaluate model as a function
% YPRED = FEVAL(M,X1,X2,...Xp) computes an array YPRED of predicted
% values from the regression model M using predictor values X1, X2, ...,
% Xp, where P is the number of predictors in M. Each X argument must be
% the same size, and must have the same type as the corresponding
% predictor variable. The size of the YPRED output is the same as the
% common size of the X inputs.
I guess this is some kind of multiple dispatch problem. How can I avoid this? My code works for other functions. I think it is only because my model variable is an instance of LinearModel. Is there a way to specifically call the main feval function, built-in (/Applications/MATLAB_R2021b.app/toolbox/matlab/lang/feval)?
More Answers (0)
Find more on Discriminant Analysis in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!Start Hunting!