Timestamp char or cell into double matrix

1 次查看(过去 30 天)
I have a script to discover missing time periods in large data sets that begin with a Timestamp (TS). The TS is in form of "yyyy-mm-dd HHMM"
The script imports the data into a cell array C:
data = cell2mat(cellfun(@str2double,C,'UniformOutput',false));
but all the functions within work on double matrix variables. This includes the output matrix, which is fprint'd into a new file.
What I need to do is retain the original TS as a character-type column in the output matrix. Since the point of the script is to fill in holes and remove duplicates, I must apply the index which maps out the missing stuff to the TS.
I have a few different ways to get the TS, but none of them is adequate. I can:
TSdnum=datenum(TS_filtered)
TSnew = datestr(TSdnum,31)
This somehow generates a timestamp which is 1 minute off. For example 2015-03-15 15:15 comes out as 2015-03-15 15:16
I can get a character array out of the cell:
TSraw = cell2mat(C{1})
but I don't know how to put this into the final array. I have used cell2string(strsplit(.... before but don't see how this can work.
I think I can make a table, and use .Format, but again, how to get into my output matrix.

采纳的回答

Steven Lord
Steven Lord 2018-11-16
The script imports the data into a cell array C:
Since from your comment on manhan ravi's answer the data is tabular in nature, consider using readtable to read the data from the file into a table array (importing the dates as datetime instead of plain text), table2timetable to convert the table into a timetable, and retime to make the times in the timetable regularly spaced. See the "Adjust Timetable by Inserting Missing Data Indicators" example on the retime function documentation page for an example that sounds similar to the application you described.
Alternately, since you already have the data in a cell array you could use cell2table instead of readtable to convert the already-imported data to a table. After that convert the text representing the dates and times in the first variable in the table into datetime using the datetime function. Finally continue as above with table2timetable and retime.
  1 个评论
Colin Edgar
Colin Edgar 2018-11-16
Thank you for the guidance. I was aware of the timetable funcitons but was intimidated to retool my script. You outlined the approach more thoroughly. These cell to mat things get pretty messy. So, I will attempt it, and thank you again.

请先登录,再进行评论。

更多回答(1 个)

madhan ravi
madhan ravi 2018-11-16
  1 个评论
Colin Edgar
Colin Edgar 2018-11-16
Thank you for the suggestion. Perhaps you could show me some further example.
I have this cell array b '14605x2'
'2013-01-02' '300'
'2013-01-02' '600'
'2013-01-02' '900'
'2013-01-02' '1200'
'2013-01-02' '1500'
'2013-01-02' '1800'
'2013-01-02' '2100'
I can use datetime:
x=datetime(b,'ConvertFrom','yyyy-mm-dd HHMM');
'2013-01-02' 'NaT'
'2013-01-02' 'NaT'
'2013-01-02' 'NaT'
'2013-01-02' 'NaT'
'2013-01-02' 'NaT'
'2013-01-02' 'NaT'
'2013-01-02' 'NaT'
Would it be easier to combine the cells first, then use 'datetime'? I don't know how to put the cell parts together into one cell, but that may work better.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Timetables 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by