フィルターのクリア

How do I add a column to table?

967 ビュー (過去 30 日間)
m m
m m 2015 年 11 月 19 日
編集済み: MathWorks Support Team 2024 年 6 月 5 日
I have created a CSV file. When I use the "readable" function to load the table, I get an 8x10 table. I want to expand the table to an 8x11 table, where the 11th column should have the values from a function I have created. How do I do this?

採用された回答

Walter Roberson
Walter Roberson 2024 年 5 月 15 日
編集済み: MathWorks Support Team 2024 年 6 月 5 日
You can add a new table variable by using either dot notation or the "addvars" function. Dot notation adds new variables to the end of table. If you use the "addvars" function, then you can choose the location of the new table variable. 
For example, this code creates a table and two column vectors. Then it adds the column vectors to the table as new variables.
LastName = ["Sanchez";"Johnson";"Li";"Diaz";"Brown"];
Age = [38;43;38;40;49];
Smoker = logical([1;0;1;0;1]);
T = table(LastName,Age,Smoker);
T.NewData = rand(5,1) % Dot notation adds new variable to end of table
MoreData = rand(5,1);
T = addvars(T,MoreData,'Before',1) % Add variable to beginning of table
If you generate a table variable name by evaluating an expression, you can pass it as the name of the new variable with either of these syntaxes. 
TodaysData = "Data " + string(datetime("today"));
T.(TodaysData) = rand(5,1)
YesterdaysData = "Data " + string(datetime("today") - 1);
T = addvars(T,rand(5,1),'NewVariableNames',YesterdaysData,'Before',1)
For more information, see the following documentation:
Add, Delete, and Rearrange Table Variables
https://www.mathworks.com/help/matlab/matlab_prog/add-and-delete-table-variables.html

その他の回答 (3 件)

Sean de Wolski
Sean de Wolski 2015 年 11 月 20 日
T.EleventhColumn = rand(8,1);
Where 'EleventhColumn' is the name you want for the variable.
  2 件のコメント
Theodore Wilkening
Theodore Wilkening 2020 年 3 月 13 日
this works best.
Yifei
Yifei 2022 年 11 月 26 日
The most convenient approach.

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


thang ngo
thang ngo 2020 年 7 月 24 日
This helps to pass a name as parameter:
name = 'new_column'
T.(name) = rand(8,1);

Ehab Abdelkarim
Ehab Abdelkarim 2018 年 8 月 3 日
you may want to check the following link https://de.mathworks.com/help/matlab/matlab_prog/add-and-delete-table-variables.html

カテゴリ

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

製品


リリース

R2024a

Community Treasure Hunt

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

Start Hunting!

Translated by