How can I convert one date and time from two colums?
Mostrar comentarios más antiguos
Im trying to convert the first two columns of a cell into a Matlab time. first column {1,1} is the date in YYYY-MM-DD format and the second it the time in HH:MM format. Any ideas where I'm going wrong?

file = 'D:\Beach Erosion and Recovery\Bournemouth\Bournemouth Tidal Data\tidal_data_jtide.txt'
fileID = fopen(file);
LT_celldata = textscan(fileID,'%D%D%D%D%d%[^\n\r]','delimiter',',');
formattime = 'yyyy-mm-dd HH:MM'
date = LT_celldata{1,1};
time = LT_celldata{1,2};
date_time = datenum('date','time');
3 comentarios
Image Analyst
el 22 de Jul. de 2018
You forgot to attach tidal_data_jtide.txt.
Come on, make it easy for us to help you.
Almost certainly '%D%D%D%D%d%[^\n\r]' isn't the right format string unless there are four columns of date info...show us the actual input format for the file first and let's try to solve the problem on import instead of patching up afterwards.
Specifically in the code you've referenced only one specific cell in the indexing expressions {1,1} and {1,2} not the columns and you've passed the literal strings 'date' and 'time' to the datenum function instead of the variables date and time.
datenum has been deprecated; use datetime instead unless your're stuck with a (by now getting quite) old release.
Lastly, when we get the file format; it probably will make a lot of sense to use readtable to create a table instead of textscan and get the benefits thereof in processing ease, or since you apparently have one, possibly converting that to a timetable or timeseries object may be "even more better"...
dpb
el 25 de Jul. de 2018
There was a subsequent Q? on how to find the time associated with the minimum by day; I posted a partial answer/comment but now I can't see that...did it go away?
BTW, as part of that, it seemed in the end keeping the date and the times as separate variables turned out to be more helpful.
Respuesta aceptada
Más respuestas (2)
Star Strider
el 22 de Jul. de 2018
One approach:
filename = 'tidal_data_jtide.txt';
TideData = readtable(filename); % Read File
Days1 = datevec(TideData.Var1); % Year-Month-Day
Hours1 = datevec(TideData.Var2); % Hour:Minute:Second
DT1 = datetime([Days1(:,1:3), Hours1(:,4:6)]); % Concatenate Date Vectors & Convert To ‘datetime’
Days2 = datevec(TideData.Var3); % Year-Month-Day
Hours2 = datevec(TideData.Var4); % Hour:Minute:Second
DT2 = datetime([Days2(:,1:3), Hours2(:,4:6)]); % Concatenate Date Vectors & Convert To ‘datetime’
TideData = table(DT1, DT2, TideData.Var5, TideData.Var6, TideData.Var7); % Result - Create New Table
Check = TideData(1:10,:) % Optional - Delete Later
3 comentarios
Jeremy Hughes
el 23 de Jul. de 2018
This could be simplified:
TideData = readtable(filename);
% time of day may or may not be needed depending on
% the types you're getting out of the file
DT1 = TideData.Var1 + timeofday(TideData.Var2);
DT2 = TideData.Var3 + timeofday(TideData.Var4);
If you're using R2018a, there are convenience functions for transforming tables. The benefit here is that it easily extends to many variables in the table without needed to explicitly name them.
TideData = removevars(TideData,1:4);
TideData = addvars(TideData,DT1,DT2,'Before',1);
Hope this helps,
Jeremy
dpb
el 23 de Jul. de 2018
I'd either forgotten or was never aware of timeofday, Jeremy...that aids somewhat in my complaint in other comment.
Jeremy Hughes
el 27 de Jul. de 2018
In R2018a, readtable should be able to get duration for text like '12:30:21' without specifying anything. If it's 'hh:mm' or 'mm:ss' you'll have to specify the right format.
Gwyn Hennessey
el 22 de Jul. de 2018
0 votos
Categorías
Más información sobre Time Series Objects en Centro de ayuda y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!