MATLAB Answers

Extracting a timestamp from a header in a text file that also contains tabular data

5 views (last 30 days)
Charles Bahr
Charles Bahr on 17 Jun 2021
Answered: Asvin Kumar on 30 Jun 2021
The .txt file I am working with should be attached.
In the second row, I have some strings that look like this:
Recording Time Stamp: 2021-06-10 18:39:44.402
The time stamp marks the beginning of when the data started being recorded. I want to extract the time at the end of it in HH:MM:SS.SSS and turn it into a time value that I can work with in Matlab. The device I have records 500 rows of data per second. I want to be able to add a column of data to the table that uses the time stamp and then adds 1/500 of a second to the time in each successive row, so that I know precisely what time each row of data was taken.
Does anybody know the simplest way this could be done? I've taken a crack at it but can't quite figure it out.

Accepted Answer

Asvin Kumar
Asvin Kumar on 30 Jun 2021
There's no single function which does this. You can easily put together a script.
  1. Use the Import Tool to read the data (Fp1, Fp2, etc.) into a table. The tool will automatically pick Tab as the delimiter.
  2. Use the Import Tool again to read the time at the top of the file. You can select Space as a column delimiter. Once that's done, modify the data type of the cell containing the time value. Provide a custom time format like: HH:mm:ss.SSS
At this point you will have access to all the data that you need. Note that you can also generate scripts for both of these operations and combine them to extract data programmatically.
Once you have the data, you can add an additional column to the table of type datetime for which values are yet to be computed. You can create this additional column by adding an array of values spaced 1/500 s apart to the extracted time value. See example on adding and subtracting durations to Datetime array. This is similar to:
t = datetime('18:39:44.402', 'Format', 'HH:mm:ss.SSS');
listOfTimes = t + milliseconds(0:2:10)
listOfTimes = 1×6 datetime array
18:39:44.402 18:39:44.404 18:39:44.406 18:39:44.408 18:39:44.410 18:39:44.412
Refer to this page on adding and remove variables from tables and the addvars function. They willl come in handy.
You might be interested in the timetable datatype which is a special type of table with time values in one of the columns.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!

Translated by