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에 대해 자세히 알아보기
태그
참고 항목
웹사이트 선택
번역된 콘텐츠를 보고 지역별 이벤트와 혜택을 살펴보려면 웹사이트를 선택하십시오. 현재 계신 지역에 따라 다음 웹사이트를 권장합니다:
또한 다음 목록에서 웹사이트를 선택하실 수도 있습니다.
사이트 성능 최적화 방법
최고의 사이트 성능을 위해 중국 사이트(중국어 또는 영어)를 선택하십시오. 현재 계신 지역에서는 다른 국가의 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)
