Ascending table column labels

1 回表示 (過去 30 日間)
Joost de Witte
Joost de Witte 2020 年 1 月 7 日
コメント済み: Star Strider 2020 年 1 月 7 日
Hello all,
I've got a table which contains 38 column's, each representing one cycle. I want to label them "cycle 1", "cycle 2", etc.
I tried something like this, but this doesn't work. Any help would be appreciated, thank you!
colnames = "Cycle" + 1:size(matrix,2);
table = array2table(matrix,'VariableNames', colnames);

採用された回答

Star Strider
Star Strider 2020 年 1 月 7 日
Use the compose function (R2016b and later versions):
colnames = compose("Cycle %d", 1:size(matrix,2));
Alternatively, use the sprintfc (undocumented) function:
colnames = sprintfc("Cycle %d", 1:size(matrix,2));
No loop necessary.
  2 件のコメント
Joost de Witte
Joost de Witte 2020 年 1 月 7 日
Thank you, it worked.
Star Strider
Star Strider 2020 年 1 月 7 日
As always, my pleasure!

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

その他の回答 (1 件)

Jesus Sanchez
Jesus Sanchez 2020 年 1 月 7 日
編集済み: Jesus Sanchez 2020 年 1 月 7 日
I would do it inside a for loop:
matrix = [1 2 3; 3 4 5; 6 4 5; 9 8 7]; % 4 rows and 3 columns
colnames = {}; % Initializes colnames
for n=1:size(matrix,2)
colnames{end+1} = ['Cycle ' num2str(n)];
end
colnames = colnames.'; % To put them in one column, for readability
Result:
ans =
3×1 cell array
{'Cycle 1'}
{'Cycle 2'}
{'Cycle 3'}
  1 件のコメント
Joost de Witte
Joost de Witte 2020 年 1 月 7 日
Thank you, did worked!

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

カテゴリ

Help Center および File ExchangeImage Data Workflows についてさらに検索

タグ

製品


リリース

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by