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

3 次查看(过去 30 天)
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 中查找有关 Linear Regression 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by