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

3 ビュー (過去 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 Center および File ExchangeLinear Regression についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by