How to add a new column to a table such that the new column has all 0s or ''?

9 ビュー (過去 30 日間)
JFz
JFz 2015 年 12 月 14 日
コメント済み: JFz 2018 年 5 月 14 日
I need to add two more columns to my table. One has all the 0s; one has all the ''. I will change the 0s and '' later.
How to add a new column to a table such that the new column has all 0s or ''?
Thanks,
Jennifer

採用された回答

Walter Roberson
Walter Roberson 2015 年 12 月 14 日
nrow = size(YourTable,1);
YourTable.NewColumnName = zeros(nrow, 1); %0
YourTable.OtherNewColumn = char(zeros(nrow,0)); %empty string, see note
YourTable.ThirdColumn = repmat({''}, nrow, 1); %cell array of empty string
Note: if you use the char(zeros(nrow,0)) then although you have initialized to empty strings, all of the columns then become constrained to be empty strings and you cannot expand those except by replacing the entire column. You probably want the cell array version rather than the empty-string version
  4 件のコメント
Peter Perkins
Peter Perkins 2018 年 5 月 14 日
More recentl (R2018a), you can preallocatre a table with a syntax such as
t = table('Size',[n 2],'VariableTypes',{'double' 'cellstr'})
Once you have that, you can horzcat or assign the new table onto the existing table.
JFz
JFz 2018 年 5 月 14 日
Thanks.

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMatrices and Arrays についてさらに検索

タグ

製品

Community Treasure Hunt

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

Start Hunting!

Translated by