Unrecognized function or variable 'predict'.

18 visualizzazioni (ultimi 30 giorni)
From what I know, the predict function ( https://www.mathworks.com/help/stats/linearmodel.predict.html ) is an build-in MATLAB function, but I'm getting the above error when calling this function. I am running R2022A; updated just cause I thought this was a new function and I need it.
Edit: the function is actually in the Statistics and Machine Learning Toolbox I think, but I already have that installed but I still get the error.
  7 Commenti
Jeremy Lin
Jeremy Lin il 14 Mar 2022
@Walter Roberson sure thats possible, but regardless what my inputs are, the error still shouldn't be that 'predict' is not recognized right?
Walter Roberson
Walter Roberson il 14 Mar 2022
No, you are wrong.
When you have a name that is not a variable name in scope, and it is being used with () syntax, then MATLAB starts looking through a heirarchy of locations to figure out what to invoke.
One of the first things it does is looks through the classes of the parameters to figure out which parameter has the dominant class; https://www.mathworks.com/help/matlab/matlab_oop/class-precedence.html . most of the time it is the type of the first parameter that dominants.
After that it looks through the methods associated with the class that was determined to dominant for the call, to see if there is a method with that name. If there is, then it calls that method.
Only after it checks for class methods does it look for non-class functions with the given name.
Thus, if you call predict() with a first parameter that is a ClassificationSVM object, then after it determines that ClassificationSVM is the dominant class, it looks through the methods of ClassificationSVM class, finds that there is a predict() function, and invokes that directly. That function does not need to do type checking if the first parameter, because class methods can only be invoked if the object is of the correct type.
If you were to call predict() with a first parameter that is a struct() then after it determines that struct is the dominant class, it looks through the methods of struct() and finds that there is no specific predict() method for class struct. At that point it starts looking for predict functions that are not within any class. Such functions do need to do type checking on their first parameter to ensure that the data is a type that can be processed. It would be up to any such predict() function to issue its own error message saying whatever (and it is common for such functions to check the number of inputs before they check the types of the inputs, so it is common to receive a misleading error about the wrong number of parameters when the real problem is that you are invoking a function that is not expecting that type of data.)
And if there is no general predict() function anywhere on the path after MATLAB determined that there is no predict() method for the appropriate dominant class, then you are given an error message about no such function for that datatype.... just like you are seeing.
In order for you to receive a different error message, something would have had to define a predict.m or predict.p or predict.slx or predict.mdl or predict.mlx or built-in function predict() that was on the path in order to receive your input and give you an error message.
Class methods never get invoked if the class is wrong: they can only get invoked when you get a class match. (Admittedly, if you have a hierarchy of classes and you do not implement all specialized and abstract classes correctly, then the class method that gets invoked might not be the one you wanted to be invoked.)

Accedi per commentare.

Risposta accettata

Walter Roberson
Walter Roberson il 14 Mar 2022
Add this to your MATLAB path
function varargout = predict(varargin)
if nargin == 0
msg = 'You have no input at all!';
no_input = true;
else
msg = sprintf('Your input is class: %s', class(varargin{1}));
no_input = false;
end
fprintf('If you are seeing this message, then you have tried to invoke\n');
fprintf('predict() for a datatype that no predict() method is defined for -- \n');
fprintf('Or else you have not fully installed or licensed the appropriate toolbox.\n');
fprintf('\n');
fprintf('%s\n\n', msg);
if ~no_input
fprintf('Available methods for your input data type are:\n');
methods(varargin{1})
fprintf('\n');
end
if license('test', 'statistics_toolbox')
fprintf('You DO have a license for Statistics and Machine Learning Toolbox\n');
else
fprintf('You do NOT have a license for Statistics and Machine Learning Toolbox\n');
end
info = ver('stats');
if ~isempty(info)
fprintf('You DO have Statistics and Machine Learning Toolbox installed, %s\n', info.Release);
else
fprintf('You do NOT have Statistics and Machine Learning Toolbox installed.\n');
end
fprintf('\n');
fprintf('predict() is a correct function for these classes:\n\n');
fprintf('* identified model: System Identification Toolbox\n');
fprintf('* ClassificationKNN: Statistics and Machine Learning Toolbox\n');
fprintf('* ClassificationSVM: Statistics and Machine Learning Toolbox\n');
fprintf('* ClassificationTree: Statistics and Machine Learning Toolbox\n');
fprintf('* CompactClassificationSVM: Statistics and Machine Learning Toolbox\n');
fprintf('* CompactClassificationTree: Statistics and Machine Learning Toolbox\n');
fprintf('* DAGNetwork: Deep Learning Toolbox\n');
fprintf('\n');
fprintf('For other kinds of objects, see the following for function hints:\n');
fprintf('<a href="https://www.mathworks.com/matlabcentral/answers/427309-classify-requires-at-least-3-arguments#answer_445205">this Answer</a>');
end
  1 Commento
Jeremy Lin
Jeremy Lin il 14 Mar 2022
not exactly the solution, but you're right, the input for the function was not right. Thanks for your help and the clarification!

Accedi per commentare.

Più risposte (1)

Image Analyst
Image Analyst il 14 Mar 2022
You probably don't have that toolbox.
What does this say
ver
which -all predict
% Check that user has the specified Toolbox installed and licensed.
% hasLicenseForToolbox = license('test', 'image_toolbox'); % Check for Image Processing Toolbox.
% hasLicenseForToolbox = license('test', 'Image_Acquisition_Toolbox'); % Check for Image Acquisition Toolbox.
hasLicenseForToolbox = license('test', 'Statistics_Toolbox'); % Check for Statistics and Machine Learning Toolbox.
% hasLicenseForToolbox = license('test', 'Signal_Toolbox'); % Check for Signal Processing Toolbox.
% hasLicenseForToolbox = license('test', 'Video_and_Image_Blockset'); % Check for Computer Vision System Toolbox.
% hasLicenseForToolbox = license('test', 'Neural_Network_Toolbox'); % Check for Deep Learning Toolbox.
if ~hasLicenseForToolbox
% User does not have the toolbox installed, or if it is, there is no available license for it.
% For example, there is a pool of 10 licenses and all 10 have been checked out by other people already.
ver % List what toolboxes the user has licenses available for.
message = sprintf('Sorry, but you do not seem to have the Stats Toolbox.\nDo you want to try to continue anyway?');
reply = questdlg(message, 'Toolbox missing', 'Yes', 'No', 'Yes');
if strcmpi(reply, 'No')
% User said No, so exit.
return;
end
end
  2 Commenti
Jeremy Lin
Jeremy Lin il 14 Mar 2022
-----------------------------------------------------------------------------------------------------
MATLAB Version: 9.12.0.1884302 (R2022a)
MATLAB License Number: 40707750
Operating System: Mac OS X Version: 10.15.6 Build: 19G2021
Java Version: Java 1.8.0_202-b08 with Oracle Corporation Java HotSpot(TM) 64-Bit Server VM mixed mode
-----------------------------------------------------------------------------------------------------
MATLAB Version 9.12 (R2022a)
Signal Processing Toolbox Version 9.0 (R2022a)
Statistics and Machine Learning Toolbox Version 12.3 (R2022a)
'predict' not found.
Walter Roberson
Walter Roberson il 14 Mar 2022
When there is a function name that is only used as a class method, and the function name does not appear as a separate .m file inside the @ definition for the class, then MATLAB is not able to find the class method until the class is loaded.
which -all predict
/MATLAB/toolbox/ident/ident/predict.m /MATLAB/toolbox/nnet/cnn/@DAGNetwork/predict.m % Shadowed DAGNetwork method
try
ClassificationSVM() %not actually valid but will load the class
catch ME
fprintf('for this demo this error is normal')
end
for this demo this error is normal
which -all predict
/MATLAB/toolbox/ident/ident/predict.m /MATLAB/toolbox/stats/classreg/+classreg/+learning/+classif/CompactClassificationSVM.m % Shadowed ClassificationSVM method /MATLAB/toolbox/stats/classreg/+classreg/+learning/+classif/ClassificationModel.m % Shadowed classreg.learning.classif.FullClassificationModel method /MATLAB/toolbox/stats/classreg/+classreg/+learning/Predictor.m % Shadowed classreg.learning.FullClassificationRegressionModel method /MATLAB/toolbox/nnet/cnn/@DAGNetwork/predict.m % Shadowed DAGNetwork method
Notice that predict was not known in some locations until the defining class was loaded.

Accedi per commentare.

Community Treasure Hunt

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

Start Hunting!

Translated by