How to Create new column in table with words in each cell

18 views (last 30 days)
xander fong
xander fong on 28 Jul 2015
Answered: Matt Cohen on 30 Jul 2015
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!

Accepted Answer

Matt Cohen
Matt Cohen on 30 Jul 2015
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

More Answers (0)

Community Treasure Hunt

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

Start Hunting!