How to read a text file with irregular timestamp data using detectImportOptions function ?

28 views (last 30 days)
I want to read the condition monitoring datas stored in the text file. The data text file has 7 colums, where the first colum represents the sample time and the rest are the sensor datas. Portion of the data is illustrated in the attached file. I have tried to use the detectImportOptions function for importing the data into MATLAB workspace, yet the detectImportOptions function can not detect the file content correctly. Which function should I use to import this data file ?
opts = detectImportOptions("sampleDataFile.txt","Delimiter"," ");
preview("sampleDataFile.txt",opts)
Warning: Column headers from the file were modified to make them valid MATLAB identifiers before creating variable names for the table. The original column headers are saved in the VariableDescriptions property.
Set 'VariableNamingRule' to 'preserve' to use the original column headers as table variable names.
ans = 8×1 table
x2010_09_12_20_48_21_00000__07000_356317_0_416615_0_0462520_066468_0_0623550_018159 _____________________________________________________________________________________________ {'2010-09-12_20:48:21.00005_-0700→0.340215→-0.142868→0.050365→-0.062355→0.002056→-0.142868' } {'2010-09-12_20:48:21.00010_-0700→0.324112→-0.319999→-0.014046→-0.078457→0.002056→0.002056' } {'2010-09-12_20:48:21.00015_-0700→0.034262→0.163084→-0.094560→-0.062355→-0.046252→0.018159' } {'2010-09-12_20:48:21.00020_-0700→0.179187→-0.158971→0.034262→0.002056→-0.062355→0.018159' } {'2010-09-12_20:48:21.00025_-0700→0.130879→-0.046252→0.018159→-0.062355→-0.014046→0.002056' } {'2010-09-12_20:48:21.00030_-0700→0.324112→-0.223382→0.082570→-0.062355→0.050365→-0.046252' } {'2010-09-12_20:48:21.00035_-0700→-0.094560→0.098673→-0.014046→-0.110663→-0.046252→0.018159'} {'2010-09-12_20:48:21.00040_-0700→0.211392→-0.175074→-0.046252→0.050365→0.002056→0.018159' }

Accepted Answer

Stephen23
Stephen23 on 26 Nov 2025 at 8:28
Edited: Stephen23 on 26 Nov 2025 at 9:20
The file that you uploaded is tab delimited, not space delimited as you specified. Once you provide the correct delimiter importing the file content will be a lot easier:
fnm = 'sampleDataFile.txt';
opt = detectImportOptions(fnm, 'Delimiter','\t');
preview(fnm,opt)
ans = 8×7 table
Var1 Var2 Var3 Var4 Var5 Var6 Var7 ___________________________________ ________ _________ _________ _________ _________ _________ {'2010-09-12_20:48:21.00000_-0700'} 0.35632 -0.41662 -0.046252 0.066468 -0.062355 0.018159 {'2010-09-12_20:48:21.00005_-0700'} 0.34021 -0.14287 0.050365 -0.062355 0.002056 -0.14287 {'2010-09-12_20:48:21.00010_-0700'} 0.32411 -0.32 -0.014046 -0.078457 0.002056 0.002056 {'2010-09-12_20:48:21.00015_-0700'} 0.034262 0.16308 -0.09456 -0.062355 -0.046252 0.018159 {'2010-09-12_20:48:21.00020_-0700'} 0.17919 -0.15897 0.034262 0.002056 -0.062355 0.018159 {'2010-09-12_20:48:21.00025_-0700'} 0.13088 -0.046252 0.018159 -0.062355 -0.014046 0.002056 {'2010-09-12_20:48:21.00030_-0700'} 0.32411 -0.22338 0.08257 -0.062355 0.050365 -0.046252 {'2010-09-12_20:48:21.00035_-0700'} -0.09456 0.098673 -0.014046 -0.11066 -0.046252 0.018159
Note that calling DETECTIMPORTOPTIONS is not required, you can simply call READTABLE directly:
tbl = readtable(fnm, 'Delimiter','\t');
tbl.Var1 = datetime(tbl.Var1, 'TimeZone','-07:00', 'InputFormat','u-M-d_H:m:s.SSSSS_Z', 'Format','u-MM-dd HH:mm:ss.SSSSS Z')
tbl = 20×7 table
Var1 Var2 Var3 Var4 Var5 Var6 Var7 _______________________________ _________ _________ _________ _________ _________ _________ 2010-09-12 20:48:21.00000 -0700 0.35632 -0.41662 -0.046252 0.066468 -0.062355 0.018159 2010-09-12 20:48:21.00005 -0700 0.34021 -0.14287 0.050365 -0.062355 0.002056 -0.14287 2010-09-12 20:48:21.00010 -0700 0.32411 -0.32 -0.014046 -0.078457 0.002056 0.002056 2010-09-12 20:48:21.00014 -0700 0.034262 0.16308 -0.09456 -0.062355 -0.046252 0.018159 2010-09-12 20:48:21.00020 -0700 0.17919 -0.15897 0.034262 0.002056 -0.062355 0.018159 2010-09-12 20:48:21.00025 -0700 0.13088 -0.046252 0.018159 -0.062355 -0.014046 0.002056 2010-09-12 20:48:21.00029 -0700 0.32411 -0.22338 0.08257 -0.062355 0.050365 -0.046252 2010-09-12 20:48:21.00035 -0700 -0.09456 0.098673 -0.014046 -0.11066 -0.046252 0.018159 2010-09-12 20:48:21.00040 -0700 0.21139 -0.17507 -0.046252 0.050365 0.002056 0.018159 2010-09-12 20:48:21.00045 -0700 -0.014046 0.14698 -0.15897 -0.078457 -0.030149 0.018159 2010-09-12 20:48:21.00050 -0700 0.37242 -0.28779 -0.09456 0.11478 0.018159 -0.09456 2010-09-12 20:48:21.00055 -0700 -0.014046 0.14698 -0.030149 -0.062355 0.050365 -0.062355 2010-09-12 20:48:21.00059 -0700 0.08257 -0.09456 -0.25559 0.066468 0.002056 0.002056 2010-09-12 20:48:21.00065 -0700 -0.078457 0.050365 -0.078457 0.098673 -0.062355 0.002056 2010-09-12 20:48:21.00070 -0700 0.11478 -0.23949 -0.15897 0.08257 -0.046252 -0.046252 2010-09-12 20:48:21.00075 -0700 0.14698 0.034262 0.098673 0.034262 -0.078457 -0.078457
Note that REDATBLE does not handle timezones, so you will have to call DATETIME afterwards.

More Answers (0)

Products


Release

R2025a

Community Treasure Hunt

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

Start Hunting!