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

3 vues (au cours des 30 derniers jours)
영훈 정
영훈 정 le 5 Avr 2023
Modifié(e) : Stephen23 le 5 Avr 2023
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..

Réponse acceptée

Luca Ferro
Luca Ferro le 5 Avr 2023
Modifié(e) : Luca Ferro le 5 Avr 2023
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.

Plus de réponses (1)

Stephen23
Stephen23 le 5 Avr 2023
Modifié(e) : Stephen23 le 5 Avr 2023
"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

Community Treasure Hunt

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

Start Hunting!

Translated by