You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
Unable to run the code given in the link: https://in.mathworks.com/help/stats/code-generation-for-prediction-of-machine-learning-model-using-matlab-coder-app.html
3 views (last 30 days)
Show older comments
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
Doli Hazarika
on 2 Mar 2022
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.
Walter Roberson
on 2 Mar 2022
There is a possibility that it is complaining about a previous line not being complete.
Doli Hazarika
on 2 Mar 2022
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, ...
Walter Roberson
on 2 Mar 2022
Edited: Walter Roberson
on 2 Mar 2022
Please show
which -all fitcensemble
dbtype fitcensemble 1:5
Doli Hazarika
on 2 Mar 2022
>> 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);
Walter Roberson
on 2 Mar 2022
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{:});
Doli Hazarika
on 4 Mar 2022
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.
Walter Roberson
on 4 Mar 2022
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
Doli Hazarika
on 7 Mar 2022
Yes it worked this way. But instead of creating a function and calling it directly why are we doing it this way?
Walter Roberson
on 7 Mar 2022
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.
Answers (0)
See Also
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list:
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)