Clear Filters
Clear Filters

Multiple dispatch problem when trying to call the feval function with a LinearModel as an argument.

4 views (last 30 days)
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(isequaln(y_sigma, nan));
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/
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/

Accepted Answer

Bill Tubbs
Bill Tubbs on 24 Feb 2023
There are some good suggested solutions to this from Cris Luengo on this parallel stackoverflow post:

More Answers (0)




Community Treasure Hunt

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

Start Hunting!