How to Create new column in table with words in each cell
3 次查看(过去 30 天)
显示 更早的评论
I have a 40x1 cell array in which each cell contains a 15x300 table. How do I add a 16th column to each table where every cell in that column contains the word "FULL"? Thanks!
0 个评论
采纳的回答
Matt Cohen
2015-7-30
Hi Xander,
I understand that you are trying to add an additional column to each table stored in a 40x1 cell array. You want this new column to contain the word "FULL" for each entry in the column. You mentioned that each table is 15x300; however, since you wish to add 16th column instead of 16th row, I will assume the tables are actually 300x15.
One way to accomplish this is by creating a new 300x1 table with "FULL" as each entry value. Then you can concatenate this table with each of the tables stored in the cell array using the "cellfun" function. "cellfun" takes in a function handle to apply to each cell array element. In the following sample code, I have defined a function which concatenates two tables and have passed its handle to the "cellfun" function. I have also assumed that the 40x1 cell array is named 'C'.
% Create a table with one column and 300 rows containing string "FULL".
fullColumn = repmat('FULL',[300,1]);
T2 = table(fullColumn);
% Create a table concatenation function handle, and apply this
% to each entry in cell array C.
table_concat = @(x) [x T2];
C_new = cellfun(table_concat, C, 'UniformOutput', false);
This same approach can be easily modified to add a 16th row if the table dimensions are in fact 15x300.
I hope this helps.
Matt
0 个评论
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrices and Arrays 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!