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

3 visualizzazioni (ultimi 30 giorni)
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..

Risposta accettata

Luca Ferro
Luca Ferro il 5 Apr 2023
Modificato: Luca Ferro il 5 Apr 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.

Più risposte (1)

Stephen23
Stephen23 il 5 Apr 2023
Modificato: Stephen23 il 5 Apr 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