フィルターのクリア

Converting table elements to individual arrays and assigning the same header

40 ビュー (過去 30 日間)
Akshay
Akshay 2023 年 1 月 25 日
コメント済み: Akshay 2023 年 1 月 26 日
I have an n x 85 table and I'm looking for a way to assign each of these individual columns to a separate array under the same existing header name (all string). table2array(T) let's me convert the table to individual arrays but I want to run a for loop in order to assign the same header to each of these newly created arrays. I've extracted the header names using the following where T is my table: table_names = T.Properties.VariableNames.
Not sure how to now assign this name to the rest of the numeric data from each column on the table.
Any help would be greatly appreciated!

採用された回答

Michael Habermann
Michael Habermann 2023 年 1 月 25 日
編集済み: Michael Habermann 2023 年 1 月 25 日
t = table();
t.col_a = [1;2;3];
t.col_b = [10;11;12]
t = 3×2 table
col_a col_b _____ _____ 1 10 2 11 3 12
column_names = t.Properties.VariableNames;
for i = 1:numel(column_names)
colname = string(column_names(i));
eval(sprintf("%s = t.(colname)", colname))
end
col_a = 3×1
1 2 3
col_b = 3×1
10 11 12
  3 件のコメント
Akshay
Akshay 2023 年 1 月 26 日
Hi Michael,
I get the following error when I try to run your code: "Table variable names must be character vectors". Not sure why you weren't seeing the same error?
Akshay
Akshay 2023 年 1 月 26 日
Seems to work on R2021b as opposed to R2017b before. Thanks for the help!

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

その他の回答 (0 件)

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by