Adding time and date column to text file

조회 수: 5 (최근 30일)
Dung Tran
Dung Tran 2021년 3월 24일
댓글: Dung Tran 2021년 3월 24일
I have a txt file with thousand of rows and 8 columns, example for 1 row is below
1111 2222 3333 4444 5555 6666 7777 8888
I want to add date and time as format below
3/24/2021 3:33:30 PM 1111 2222 3333 4444 5555 6666 7777 8888
Logging started 3/24/2021 at 3:33:30 and it logged after every 2 second. so the time should increase by 2 second for each row until the last n row.
3/24/2021 3:33:30 PM 1111 2222 3333 4444 5555 6666 7777 8888
3/24/2021 3:33:32 PM 1312 2003 3133 4423 5232 6123 7712 8812
.
.
.
3/25/2021 1:12:15 AM 3132 3123 5645 3242 3546 1321 1313 1215 ( this an example of the last ending row)
Please help me in this case. thank you very much.

답변 (1개)

Adam Danz
Adam Danz 2021년 3월 24일
  1. Read in the file
  2. Create vector of datetime values (see below)
  3. Create a time table with the datetime vector and data from the file
  4. Use writetimetable to write the data back into a text file.
To create the datetime vector
n is the number of rows of data which you can get after reading in the file.
n = 1000;
startingDate = datetime(2021,3,24,15,33,30,'Format','MM/dd/yyyy hh:mm:ss a');
t = startingDate + seconds(0:2:n*2-1)'
t = 1000×1 datetime array
03/24/2021 03:33:30 PM 03/24/2021 03:33:32 PM 03/24/2021 03:33:34 PM 03/24/2021 03:33:36 PM 03/24/2021 03:33:38 PM 03/24/2021 03:33:40 PM 03/24/2021 03:33:42 PM 03/24/2021 03:33:44 PM 03/24/2021 03:33:46 PM 03/24/2021 03:33:48 PM 03/24/2021 03:33:50 PM 03/24/2021 03:33:52 PM 03/24/2021 03:33:54 PM 03/24/2021 03:33:56 PM 03/24/2021 03:33:58 PM 03/24/2021 03:34:00 PM 03/24/2021 03:34:02 PM 03/24/2021 03:34:04 PM 03/24/2021 03:34:06 PM 03/24/2021 03:34:08 PM 03/24/2021 03:34:10 PM 03/24/2021 03:34:12 PM 03/24/2021 03:34:14 PM 03/24/2021 03:34:16 PM 03/24/2021 03:34:18 PM 03/24/2021 03:34:20 PM 03/24/2021 03:34:22 PM 03/24/2021 03:34:24 PM 03/24/2021 03:34:26 PM 03/24/2021 03:34:28 PM
  댓글 수: 1
Dung Tran
Dung Tran 2021년 3월 24일
that works really good. Thank you Mr. Adam

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Dates and Time에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by