Unrecognized function or variable 'predict'.
Afficher commentaires plus anciens
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 commentaires
Walter Roberson
le 14 Mar 2022
predict() is a class method; you have to apply it to objects of the correct class(es)
Jeremy Lin
le 14 Mar 2022
Modifié(e) : Walter Roberson
le 14 Mar 2022
KSSV
le 14 Mar 2022
@Jeremy Lin is that the full error? I don't think it is the complete error which MATLAB threw..
Jeremy Lin
le 14 Mar 2022
Walter Roberson
le 14 Mar 2022
SVM structures were, if I recall correctly, the third-party SVM library.
predict() does not operate on SVM structures: it operates on objects of various classes.
Jeremy Lin
le 14 Mar 2022
Walter Roberson
le 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.)
Réponse acceptée
Plus de réponses (1)
Image Analyst
le 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 commentaires
Jeremy Lin
le 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
try
ClassificationSVM() %not actually valid but will load the class
catch ME
fprintf('for this demo this error is normal')
end
which -all predict
Notice that predict was not known in some locations until the defining class was loaded.
Catégories
En savoir plus sur Gaussian Process Regression dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!