How can i use string values saved in a cell array as variable names in a for loop.
11 次查看(过去 30 天)
显示 更早的评论
I would like to use a cell array string value as a variable name in a for loop. So every iteration there would a new variable created from the cell string value. also is it posible the change/append some text to the variable name each iteration. I think its much easier to look at the code and see what i'm trying to acomplish.
Dist_type = {'Kernel', 'Gamma', 'Normal', 'Weibull', 'Stable', 'Logistic', 'GeneralizedExtremeValue'};
p = '_pdf';
c = '_cdf';
for i = 1 : numel(Dist_type)
dis_obj = fitdist(testData, Dist_type{:,i});
% Generate pdf and cdf values and assign them to variables j and k
j= pdf(dis_obj, testData);
k= cdf(dis_obj, testData);
% Somehow rename j and k to the specific dist_type name and append p = "_pdf" and c = "cdf" at the end. As an example in the
% first iteration the two variables created would be "kernel_pdf" and "Kernel_cdf" so
% this loop would generate a separate pdf and cdf for each distribution type.
% j = append(Dist_type{:,i},p);
% k = append(Dist_type{:,i},c);
% Combine testData with each pdf and cdf values into a table
% j = table(testData, );
% k = table(testData, );
0 个评论
采纳的回答
Chunru
2021-11-29
You can put the result in a struct with field names corresponding to your Dist_type
load Test_Data.mat
Dist_type = {'Kernel', 'Gamma', 'Normal', 'Weibull', 'Stable', 'Logistic', 'GeneralizedExtremeValue'};
p = '_pdf';
c = '_cdf';
for i = 1 : numel(Dist_type)
dis_obj.(Dist_type{i}) = fitdist(testData, Dist_type{i});
end
dis_obj
更多回答(1 个)
Walter Roberson
2021-11-29
We recommend against that.
Instead, use normal variable names in your loop, but when you generate your table() then use the 'VariableNames' property to set the name of the columns as required.
If you want to create a number of these tables, then either put them into a cell array, or put them as fields in a struct (using dynamic field names)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Cell Arrays 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!