how can i use struct to "for" when i use predictFcn

조회 수: 1 (최근 30일)
영훈 정
영훈 정 2023년 4월 5일
편집: Stephen23 2023년 4월 5일
i want to predict by use "for"
i have 15 number of sturct
how can i use "for"..?
for modelname = [Tree Linear InteractionsLinear RobustLinear StepwiseLinear LinearSVM QuadraticSVM CubicSVM FinegaussianSVM MediumGaussianSVM CoarseFaussianSVM RationalQuadraticGPR SquaredExponentialGPR Matern52GPR ExponentialGPR]
var1=data_1(:);
var2=data_2(:);
insertdata=table(var1,var2);
predictdata=modelname.predictFcn(insertdata)
end
this code didn't work..

채택된 답변

Luca Ferro
Luca Ferro 2023년 4월 5일
편집: Luca Ferro 2023년 4월 5일
You should have all the model names in an array, then loop using a proper index and eval the expression.
modelNames=["Tree", "Linear", "InteractionsLinear", "RobustLinear", "StepwiseLinear", "LinearSVM", "QuadraticSVM", "CubicSVM", "FinegaussianSVM" ,"MediumGaussianSVM","CoarseFaussianSVM","RationalQuadraticGPR" ,"SquaredExponentialGPR" ,"Matern52GPR","ExponentialGPR"];
var2=data_2(:);
insertdata=table(var1,var2);
for nn=1:size(modelNames,2)
predictdata=eval(strcat(modelNames(nn),'.predictFcn(insertdata)'));
end
Probably there is a better way using dynamic field naming for structs but i could not implement it in a short time.

추가 답변 (1개)

Stephen23
Stephen23 2023년 4월 5일
편집: Stephen23 2023년 4월 5일
"i have 15 number of sturct"
And that is a problem which is best solved by putting them into one array (which they clearly should have been right from the start). It is best to avoid slow, complex, inefficient, evil EVAL.
For example, assuming that all of those structures are scalar with the same fieldnames:
S = [Tree,Linear,InteractionsLinear,RobustLinear,StepwiseLinear,LinearSVM,QuadraticSVM,CubicSVM,FinegaussianSVM,MediumGaussianSVM,CoarseFaussianSVM,RationalQuadraticGPR,SquaredExponentialGPR,Matern52GPR,ExponentialGPR];
for k = 1:numel(S)
..
S(k).predictFcn(insertdata)
..
end

카테고리

Help CenterFile Exchange에서 Matrices and Arrays에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by