When I use codegen, I get an error: Unable to delete elements from this array because dimension 2 has fixed size

조회 수: 3 (최근 30일)
I am following a guide for generate c/c++ code from Code Generation and Classification Learner App.
The guide give me the descrption at Define Entry-Point Function:
An entry-point function is a function you define for code generation. Because you cannot call any function at the top level using codegen, you must define an entry-point function that calls code-generation-enabled functions, and then generate C/C++ code for the entry-point function by using codegen.
In your current folder, define a function named mypredictCL.m that:
  • Accepts a numeric matrix (X) of raw observations containing the same predictor variables as the ones passed into Classification Learner
  • Loads the classification model in ClassificationLearnerModel.mat and the model parameters in ModelParameters.mat
  • Removes the predictor variables corresponding to the indices in removeVars
  • Transforms the remaining predictor data using the PCA centers (pcaCenters) and coefficients (pcaCoefficients) estimated by Classification Learner
  • Returns predicted labels using the model
Now I have written an entry-point function as:
function label = mypredictCL(X)
mdl = loadLearnerForCoder('ClassificationLearnerModel.mat'); % load the Classification model
args = coder.load('ModelParameters.mat'); % load the parameter
X(:,args.removeVars) = []; % remove the variables as the guide says
X = bsxfun(@minus,X,args.pcaCenters)*args.pcaCoefficients; preprocess the data
label = predict(mdl,X);
end
And then, I run the code in command window:
p = size(creditrating,2) - 1;
x = coder.typeof(0,[Inf,p],[1 0]);
codegen mypredictCL -args x
I get the error: Unable to delete elements from this array because dimension 2 has fixed size 7.
I want to know what is the right way to remove the variables in entry-point function?

답변 (1개)

Sakshi Sharma
Sakshi Sharma 2023년 11월 7일
You can modify and write your entry point function in this way:
function label = mypredictCL(X)
mdl = loadLearnerForCoder('ClassificationLearnerModel.mat'); % load the Classification model
args = coder.load('ModelParameters.mat'); % load the parameter
y = X;
y(:,args.removeVars) = []; % remove the variables as the guide says
y = bsxfun(@minus,y,args.pcaCenters)*args.pcaCoefficients; %preprocess the data
label = predict(mdl,y);
end
X is defined at compile time and has fixed dimensions, so it cannot be modified inside the code.

카테고리

Help CenterFile Exchange에서 Code Generation에 대해 자세히 알아보기

제품


릴리스

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by