Clear Filters
Clear Filters

Insert data to table

7 views (last 30 days)
Indrani
Indrani on 24 Oct 2023
Commented: Indrani on 26 Oct 2023
Hi,
I have a table temps with 2 columns of type DateTime and Double. In the Double column of the table, I have manually inserted from values (the first 90 rows). I want to populate the next rows of the Double column with values from a different table which has only 1 column.
How do I proceed?
Thanks!

Accepted Answer

Voss
Voss on 24 Oct 2023
temps = table(datetime(rand(10,1),'ConvertFrom','datenum'),rand(10,1))
temps = 10×2 table
Var1 Var2 _____________________ _________ 31-Dec--0001 05:25:55 0.5695 31-Dec--0001 05:53:09 0.0014856 31-Dec--0001 00:43:32 0.97354 31-Dec--0001 01:51:05 0.3349 31-Dec--0001 10:14:34 0.39485 31-Dec--0001 13:05:17 0.47122 31-Dec--0001 10:17:15 0.39123 31-Dec--0001 22:40:54 0.32628 31-Dec--0001 01:09:12 0.4325 31-Dec--0001 21:27:43 0.73325
a_different_table = table([1;2;3;4;5])
a_different_table = 5×1 table
Var1 ____ 1 2 3 4 5
start_row = 6;
temps{start_row+(0:size(a_different_table,1)-1),2} = a_different_table{:,1}
temps = 10×2 table
Var1 Var2 _____________________ _________ 31-Dec--0001 05:25:55 0.5695 31-Dec--0001 05:53:09 0.0014856 31-Dec--0001 00:43:32 0.97354 31-Dec--0001 01:51:05 0.3349 31-Dec--0001 10:14:34 0.39485 31-Dec--0001 13:05:17 1 31-Dec--0001 10:17:15 2 31-Dec--0001 22:40:54 3 31-Dec--0001 01:09:12 4 31-Dec--0001 21:27:43 5
  3 Comments
Voss
Voss on 24 Oct 2023
Edited: Voss on 24 Oct 2023
"I do not want a separate column Var3. Is it possible to enter values of table T2 in the column Var2, after the last value of table T1?"
Using the code from my answer and setting the start_row to be size(T1,1)+1, i.e., start right after the last value of T1.
T1 = table(datetime(rand(7,1),'ConvertFrom','datenum'),rand(7,1))
T1 = 7×2 table
Var1 Var2 _____________________ _______ 31-Dec--0001 09:58:55 0.29951 31-Dec--0001 20:12:04 0.82653 31-Dec--0001 06:15:31 0.1411 31-Dec--0001 12:22:05 0.96888 31-Dec--0001 14:49:06 0.92325 31-Dec--0001 07:30:46 0.18036 31-Dec--0001 15:18:23 0.33511
T2 = table([1;2;3])
T2 = 3×1 table
Var1 ____ 1 2 3
start_row = size(T1,1)+1;
T1{start_row+(0:size(T2,1)-1),2} = T2{:,1}
Warning: The assignment added rows to the table, but did not assign values to all of the table's existing variables. Those variables are extended with rows containing default values.
T1 = 10×2 table
Var1 Var2 _____________________ _______ 31-Dec--0001 09:58:55 0.29951 31-Dec--0001 20:12:04 0.82653 31-Dec--0001 06:15:31 0.1411 31-Dec--0001 12:22:05 0.96888 31-Dec--0001 14:49:06 0.92325 31-Dec--0001 07:30:46 0.18036 31-Dec--0001 15:18:23 0.33511 NaT 1 NaT 2 NaT 3
Indrani
Indrani on 26 Oct 2023
Thanks!

Sign in to comment.

More Answers (0)

Categories

Find more on Tables in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!