How to rename TABLE column by the combination of Character and number?

2 ビュー (過去 30 日間)
balandong
balandong 2017 年 8 月 2 日
コメント済み: Star Strider 2017 年 8 月 2 日
Dear all, pertaining of the above subject. I have the following code
c=1
for threshold = 0.5:0.01:0.6
for i =1:2
Table (c) = SomeFunction
end
% See line A
Table.Properties.VariableNames{c} = NewName
c=c+1
end
To have a new column name after each iteration of the threshold loop, I plan to rename the column name, for example, the column 1 and 2 is threshold_0_5 and threshold_0_51. The reason why threshold_0_5 and threshold_0_5 1 because MATLAB prohibit naming the column as threshold_0.5 & threshold_0.5.
By looking at the other thread,
possible conversion was
Data = strrep(threshold , '.', '_');
NewName = strcat ('threshold ',Data)
OR
NewName = strcat('Th',num2str(threshold)),
However, I got an error. May I know where did I do wrong?
Thanks in advance

採用された回答

Star Strider
Star Strider 2017 年 8 月 2 日
I am not certain what result you want. All table variable names have to be valid MATLAB variable names, so decimal points are not permitted.
Try this:
threshold = 0.5:0.01:0.6;
NamesVct = sprintf('Th_%2.0f\n',threshold*100);
NewName = regexp(NamesVct, '\n', 'split');
NewName = NewName(1:end-1);
The last line is necessary because the regexp call produces an empty cell at the end.
  4 件のコメント
balandong
balandong 2017 年 8 月 2 日
It works like a charm. Thanks SS
Star Strider
Star Strider 2017 年 8 月 2 日
As always, my pleasure.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by