add column of sequential numbers to table

11 views (last 30 days)
I have an 1870x12 tabl, I want to add a 13th column that just sequentially counts (e.i. cell 1 reads 1, cell 2 reads 2, etc all the way to 1870). Not sure how to accomplish this, I previously had it so that it created a new column with just zeros, but not sure how to get it to be a sequential count instead. Thanks for the help!!
T.(name) = zeros(1870,1); %to make new column of just zeros where original table is T

Accepted Answer

Voss
Voss on 20 Jul 2022
Here's one way to do it (shown using a smaller table):
T = array2table(rand(15,4))
T = 15×4 table
Var1 Var2 Var3 Var4 ________ ________ ________ ________ 0.74338 0.1352 0.63025 0.48214 0.02299 0.35028 0.93461 0.35399 0.46931 0.90706 0.36703 0.13832 0.55901 0.80218 0.025169 0.7931 0.4281 0.42508 0.65878 0.94011 0.41303 0.49159 0.68385 0.10559 0.38813 0.84224 0.60001 0.18712 0.2503 0.82437 0.9315 0.22304 0.32831 0.034636 0.16947 0.55141 0.76743 0.26209 0.34005 0.87865 0.3409 0.55099 0.99775 0.93093 0.051406 0.42341 0.67854 0.059411 0.27198 0.67174 0.46781 0.53463 0.010137 0.29492 0.90293 0.68013 0.73003 0.89482 0.53096 0.32032
T.new_column = (1:size(T,1)).'
T = 15×5 table
Var1 Var2 Var3 Var4 new_column ________ ________ ________ ________ __________ 0.74338 0.1352 0.63025 0.48214 1 0.02299 0.35028 0.93461 0.35399 2 0.46931 0.90706 0.36703 0.13832 3 0.55901 0.80218 0.025169 0.7931 4 0.4281 0.42508 0.65878 0.94011 5 0.41303 0.49159 0.68385 0.10559 6 0.38813 0.84224 0.60001 0.18712 7 0.2503 0.82437 0.9315 0.22304 8 0.32831 0.034636 0.16947 0.55141 9 0.76743 0.26209 0.34005 0.87865 10 0.3409 0.55099 0.99775 0.93093 11 0.051406 0.42341 0.67854 0.059411 12 0.27198 0.67174 0.46781 0.53463 13 0.010137 0.29492 0.90293 0.68013 14 0.73003 0.89482 0.53096 0.32032 15
  3 Comments
Tu DoThanh
Tu DoThanh on 21 Oct 2022
Dear sir, how to put the new column to the front of the table - i mean in the first column

Sign in to comment.

More Answers (0)

Categories

Find more on Tables in Help Center and File Exchange

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!