Insert data to table
現在この質問をフォロー中です
- フォローしているコンテンツ フィードに更新が表示されます。
- コミュニケーション基本設定に応じて電子メールを受け取ることができます。
エラーが発生しました
ページに変更が加えられたため、アクションを完了できません。ページを再度読み込み、更新された状態を確認してください。
古いコメントを表示
0 投票
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
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
2023 年 10 月 24 日
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
Indrani
2023 年 10 月 26 日
Thanks!
その他の回答 (0 件)
カテゴリ
ヘルプ センター および File Exchange で Tables についてさらに検索
タグ
参考
Web サイトの選択
Web サイトを選択すると、翻訳されたコンテンツにアクセスし、地域のイベントやサービスを確認できます。現在の位置情報に基づき、次のサイトの選択を推奨します:
また、以下のリストから Web サイトを選択することもできます。
最適なサイトパフォーマンスの取得方法
中国のサイト (中国語または英語) を選択することで、最適なサイトパフォーマンスが得られます。その他の国の 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)
