Unable to run the code given in the link: https://in​.mathworks​.com/help/​stats/code​-generatio​n-for-pred​iction-of-​machine-le​arning-mod​el-using-m​atlab-code​r-app.html

Hi,
I have been trying to run the code given the following link: https://in.mathworks.com/help/stats/code-generation-for-prediction-of-machine-learning-model-using-matlab-coder-app.html. Currently I am using Matlab2021a version. Do I need to install 2021b to run this code or am I missing something else?

15 Comments

While running the following line of code:
Mdl = fitcensemble(X,Y,'Method','Subspace','NPredToSample',5, ...
'Learners',learner,'NumLearningCycles',13);
it is showing an error stating:
Invalid expression. Check for missing multiplication operator, missing or unbalanced delimiters, or other
syntax error. To construct matrices, use brackets instead of parentheses.
There is a possibility that it is complaining about a previous line not being complete.
load ionosphere
rng('default') % For reproducibility
learner = templateKNN('NumNeighbors',2);
Mdl = fitcensemble(X,Y,'Method','Subspace','NPredToSample',5, ...
'Learners',learner,'NumLearningCycles',13);
this are the lines of code that I am running. It shows the following error:
File: fitcensemble.m Line: 1 Column: 33
Invalid expression. Check for missing multiplication operator, missing or unbalanced delimiters, or other
syntax error. To construct matrices, use brackets instead of parentheses.
Error in Classification (line 4)
Mdl = fitcensemble(X,Y,'Method','Subspace','NPredToSample',5, ...
>> which -all fitcensemble
E:\Doli\MATLAB codes\fitcensemble.m
C:\Program Files\MATLAB\R2021a\toolbox\stats\classreg\fitcensemble.m % Shadowed
>> dbtype fitcensemble 1:5
1 function Mdl = fitcensemble(X,Y,'Bag')
2 %UNTITLED Summary of this function goes here
3 % Detailed explanation goes here
4 rng('default')
5 t = templateTree('Reproducible', true);
Your E:\Doli\MATLAB codes\fitcensemble.m is interfering with calling the Mathworks fitcensemble function.
function Mdl = fitcensemble(X,Y,'Bag')
That is not valid syntax in defining a function. If you want to force Bag as the third parameter then you would want to do something like
function Mdl = my_fitcensemble(X,Y,varargin)
Mdl = fitcensemble(X, Y, 'Bag', varargin{:});
I tried but it is still showing the following error. I am unable to understand from the error
Maximum recursion limit of 500 reached.
Error in fitcensemble (line 7)
Mdl = fitcensemble(X,Y,'Bag',varargin{:});
Caused by:
Maximum recursion limit of 500 reached.
Notice that you need to rename your function and the file it is in.
It does work.
%illustration that the code DOES work when saved to a file name that does
%not conflict
%first write the code into a .m file
PROGRAM = "load ionosphere\nrng('default') %% For reproducibility\nlearner = templateKNN('NumNeighbors',2);\nMdl = fitcensemble(X,Y,'Method','Subspace','NPredToSample',5, ...\n 'Learners',learner,'NumLearningCycles',13);";
fid = fopen('my_fitcensemble.m', 'w');
fprintf(fid, PROGRAM);
fclose(fid);
%now verify that the code looks like we expect
dbtype my_fitcensemble
1 load ionosphere 2 rng('default') % For reproducibility 3 learner = templateKNN('NumNeighbors',2); 4 Mdl = fitcensemble(X,Y,'Method','Subspace','NPredToSample',5, ... 5 'Learners',learner,'NumLearningCycles',13);
%now run the code
my_fitcensemble
%check the results
whos
Name Size Bytes Class Attributes Description 5x79 790 char Mdl 1x1 613095 classreg.learning.classif.ClassificationEnsemble PROGRAM 1x1 562 string X 351x34 95472 double Y 351x1 37206 cell ans 1x1 8 double fid 1x1 8 double learner 1x1 2078 classreg.learning.FitTemplate
Yes it worked this way. But instead of creating a function and calling it directly why are we doing it this way?
I did it that way to prove that if you had a file with that content that it would work. All you need is the my_fitensemble file, not the code that creates it.

Sign in to comment.

Answers (0)

Categories

Find more on Programming Utilities in Help Center and File Exchange

Asked:

on 2 Mar 2022

Commented:

on 8 Mar 2022

Community Treasure Hunt

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

Start Hunting!