Insert data to table

7 次查看(过去 30 天)
Indrani
Indrani 2023-10-24
评论: Indrani 2023-10-26
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!

采纳的回答

Voss
Voss 2023-10-24
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 个评论
Voss
Voss 2023-10-24
编辑:Voss 2023-10-24
"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

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Tables 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by