Insert data to table
显示 更早的评论
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!
采纳的回答
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 个评论
The code in my answer places the values from the second table into the second column of the first table starting with the specified start_row. If the values from the second table would extend the first table when placed in it, then that's what happens (the code is the same); if the values from the second table would not reach to the bottom of the first table when placed there then that's what happens (the code is still the same). Here are some additional examples so you can see better how it works.
Case 1: adding values extends the table:
temps = table(datetime(rand(10,1),'ConvertFrom','datenum'),rand(10,1))
temps = 10×2 table
Var1 Var2
_____________________ ________
31-Dec--0001 01:51:10 0.21545
31-Dec--0001 08:39:59 0.30481
31-Dec--0001 01:18:19 0.20133
31-Dec--0001 14:25:10 0.7748
31-Dec--0001 15:55:34 0.40753
31-Dec--0001 14:06:46 0.90364
31-Dec--0001 04:29:18 0.4439
31-Dec--0001 10:31:34 0.47939
31-Dec--0001 17:24:58 0.31158
31-Dec--0001 08:22:42 0.042024
a_different_table = table([1;2;3;4;5;6])
a_different_table = 6×1 table
Var1
____
1
2
3
4
5
6
start_row = 6;
temps{start_row+(0:size(a_different_table,1)-1),2} = a_different_table{:,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.
temps = 11×2 table
Var1 Var2
_____________________ _______
31-Dec--0001 01:51:10 0.21545
31-Dec--0001 08:39:59 0.30481
31-Dec--0001 01:18:19 0.20133
31-Dec--0001 14:25:10 0.7748
31-Dec--0001 15:55:34 0.40753
31-Dec--0001 14:06:46 1
31-Dec--0001 04:29:18 2
31-Dec--0001 10:31:34 3
31-Dec--0001 17:24:58 4
31-Dec--0001 08:22:42 5
NaT 6
Case 2: adding values does not extend the table:
temps = table(datetime(rand(10,1),'ConvertFrom','datenum'),rand(10,1))
temps = 10×2 table
Var1 Var2
_____________________ ________
31-Dec--0001 18:06:52 0.15185
31-Dec--0001 23:43:46 0.25475
31-Dec--0001 15:50:34 0.14091
31-Dec--0001 05:20:51 0.77864
31-Dec--0001 20:00:06 0.61666
31-Dec--0001 06:57:24 0.063698
31-Dec--0001 07:49:50 0.41166
31-Dec--0001 12:17:34 0.92858
31-Dec--0001 19:35:11 0.36186
31-Dec--0001 07:24:06 0.66432
a_different_table = table([1;2;3;4])
a_different_table = 4×1 table
Var1
____
1
2
3
4
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 18:06:52 0.15185
31-Dec--0001 23:43:46 0.25475
31-Dec--0001 15:50:34 0.14091
31-Dec--0001 05:20:51 0.77864
31-Dec--0001 20:00:06 0.61666
31-Dec--0001 06:57:24 1
31-Dec--0001 07:49:50 2
31-Dec--0001 12:17:34 3
31-Dec--0001 19:35:11 4
31-Dec--0001 07:24:06 0.66432
"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
Thanks!
更多回答(0 个)
类别
在 帮助中心 和 File Exchange 中查找有关 Tables 的更多信息
标签
另请参阅
选择网站
选择网站以获取翻译的可用内容,以及查看当地活动和优惠。根据您的位置,我们建议您选择:。
您也可以从以下列表中选择网站:
如何获得最佳网站性能
选择中国网站(中文或英文)以获得最佳网站性能。其他 MathWorks 国家/地区网站并未针对您所在位置的访问进行优化。
美洲
- América Latina (Español)
- Canada (English)
- United States (English)
欧洲
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
