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!