Add column to several tables in a function

3 views (last 30 days)
Vlatko Milic
Vlatko Milic on 5 Jan 2022
Answered: Rishabh Singh on 9 Feb 2022
Hi,
I have three tables titled "Table 1, Table 2 and Table 3". Each table consists of 9 columns and approximately 10,000 rows. I want to create a new column in each table (based on the division of two columns) while maintaining the table names (and also variable names) after processing. Each column in the tables is today defined as 10000x1 double.
My initial idea is to create a function where I am performing this task, see below:
C = {Table1,Table2,Table3}
%%
for k = 1:numel(C)
[Output(C,:)] = my_calc(C{k});
end
However, I get the error "A table row subscript must be a numeric array containing real positive integers, a logical array, a character vector, a string array, or a cell array of character vectors." Is this because of the use of doubles?(see comment above)
Thank you in advance!

Answers (1)

Rishabh Singh
Rishabh Singh on 9 Feb 2022
Hi,
It seems you want to concatenate a table. Refer to following example for better understanding.
LastName = ["Sanchez";"Johnson";"Zhang";"Diaz";"Brown"];
Age = [38;43;38;40;49];
Smoker = [true;false;true;false;true];
Height = [71;69;64;67;64];
Weight = [176;163;131;133;119];
patients = table(LastName,Age,Smoker,Height,Weight) % table size is 5x5 table
BloodPressure = [124 93; 109 77; 125 83; 117 75; 122 80];
patients = [patients table(BloodPressure)] % adding a column, converting the table to 5x6 size.
Hope this helps.

Community Treasure Hunt

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

Start Hunting!