Extract Variable/column with variable name from table

565 ビュー (過去 30 日間)
yvesvincent abgrall
yvesvincent abgrall 2019 年 9 月 16 日
編集済み: Stephen23 2022 年 10 月 15 日
Hello,
I don't know how to extract a column with its variable name from a Matlab table.
I want to extract columns and not have to rename them afterwards in order to recreate a similar table bbut with only the column I want.
I need to extract them one at the time and the data types are different from one column to another.
I've tried different ways and read the matlab documentation but haven't found any solution.
Could you please help me? :)

採用された回答

Steven Lord
Steven Lord 2019 年 9 月 16 日
編集済み: Steven Lord 2019 年 9 月 16 日
In what form do you want the extracted variables to be stored?
A = magic(4);
T = array2table(A) % Variable names A1 through A4
smallerTable1 = T(:, {'A2', 'A4'}) % Smaller table with only two variables
smallerTable2 = T(:, [2 4]) % Smaller table with only two variables
vector1 = T.A2 % Double array, no variable names
vector2 = T{:, {'A2'}} % Double array, no variable names
vector3 = T{:, 2} % Double array, no variable names
If you want to process each variable from your table in turn, but you want to perform the same operation on each, consider using varfun.
  3 件のコメント
Dylan Gomes
Dylan Gomes 2022 年 10 月 13 日
This is so clunky, is there really not a way to directly call vectors within a table in Matlab (like T.A2 or T$A2)?
Stephen23
Stephen23 2022 年 10 月 13 日
編集済み: Stephen23 2022 年 10 月 15 日
"This is so clunky, is there really not a way to directly call vectors within a table in Matlab (like T.A2 or T$A2)?"
Here it is shown in Steven Lord's answer above:

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

その他の回答 (1 件)

Bob Thompson
Bob Thompson 2019 年 9 月 16 日
I believe you can get this as a return.
T.Properties.VariableNames % T is the table variable
Additionally, because you're putting them into another table, if you just extract the specific columns you want the new table, and they will carry the variable names with them.
>> A = randi(100,10,4);
>> A = array2table(A);
>> A.Properties.VariableNames
ans =
1×4 cell array
{'A1'} {'A2'} {'A3'} {'A4'}
>> B = A(:,[2,4]);
>> B.Properties.VariableNames
ans =
1×2 cell array
{'A2'} {'A4'}
  2 件のコメント
yvesvincent abgrall
yvesvincent abgrall 2019 年 9 月 16 日
Thank you for your answer Bob Nbob.
Using Properties.VariablesNames doesn't extract the data of the column.
I need to extract both the data and the variable name.
Bob Thompson
Bob Thompson 2019 年 9 月 16 日
As I mentioned, if you are extracting the column data into another table then the variable name will follow with it. Just put the information into a table class variable, which can be done most easily by indexing the portion of the original table that you want into a clean variable.

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

カテゴリ

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