import data from text file

I have a text file of values generated by an elvis data collection board. I am trying to import the values into matlab but I cannot get it organized properly. I am trying to get just the time in seconds (including decimal values) and the voltage (the last value in each line) but it is not coming in correctly. I have tried the importdata and readmatrix without any luck. I would manually enter the data but there is 152000 lines. I have only been able to read the voltage values so far. I have the file as a .csv and a .txt but they both have the same results.
Time Dev1 (NI ELVIS II+)/ai0
9/13/2022 08:25:02.307736 -4.046230E+0
9/13/2022 08:25:02.307836 -4.046230E+0
9/13/2022 08:25:02.307936 -4.045264E+0
9/13/2022 08:25:02.308036 -4.044619E+0
9/13/2022 08:25:02.308136 -4.044941E+0
9/13/2022 08:25:02.308236 -4.044297E+0
9/13/2022 08:25:02.308336 -4.044941E+0
9/13/2022 08:25:02.308436 -4.045908E+0
9/13/2022 08:25:02.308536 -4.045586E+0
9/13/2022 08:25:02.308636 -4.044619E+0
9/13/2022 08:25:02.308736 -4.045586E+0
9/13/2022 08:25:02.308836 -4.044941E+0
9/13/2022 08:25:02.308936 -4.044941E+0
9/13/2022 08:25:02.309036 -4.045264E+0

 Accepted Answer

Walter Roberson
Walter Roberson on 13 Sep 2022
If you readtable() then with that data, it should detect 3 columns, with the first one being datetime() and the second being duration() and the third being data.
Create a new variable that is the Time (datetime) plus the duration; you might want to set as Format for the result.
Now if you take the datetimes minus the first datetime in the variable, then you will get duration relative to the beginning of the table. You can then ask for seconds() of that to get the duration in seconds relative to the beginning of the table.

3 Comments

dpb
dpb on 13 Sep 2022
Edited: dpb on 14 Sep 2022
@Walter Roberson is spot on, as per usual... :)
Just to amplify on his response, the following ought to work "right out of the box" (sans typos)...
tElvis=readtable('yourfile.txt','numheaderlines',1,'readvariablenames',0);
tElvis.Properties.VariableNames={'Date','Time','V'};
tElvis=addvar(tElvis.Date+tElvis.Time,'Before','V','NewVariableNames',{'DateTime'});
tElvis=addvar(seconds(tElvis.Time-tElvis.Time(1)),'Before','V','NewVariableNames',{'t'});
The header line doesn't match up to good variable names so the above just ignores it and names them at will. It then does create the date/time and elapsed time variables as Walter suggests...
You then use those as shown above -- with the "dot" notation to address the variables of interest -- if you wanted to plot the time series, that would then look like
plot(tElvis.t,tElvis.V)
xlabel('time (s)'); ylabel('Volts')
Can't get much simpler...see the <readtable> doc for all the nuances there and then read the section on tables in the DataTypes section -- it has a full section on addressing and using tables. With a time field, this is also a candidate for a <timetable>
numheaderlies ? 😁
I knew there'd be a typo somewhere, Walter...good catch, thanks!

Sign in to comment.

More Answers (0)

Categories

Products

Release

R2022a

Asked:

on 13 Sep 2022

Edited:

dpb
on 14 Sep 2022

Community Treasure Hunt

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

Start Hunting!