Matlab dates and time not aligning after combining
2 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Callie
el 14 de Nov. de 2023
Comentada: Peter Perkins
el 16 de Nov. de 2023
My end goal is to plot discharge over time with a large dataset. The sample interval for discharge is in minutes and changes from every 5 hours to every 5 minutes a couple years into data collection. I have created a table containing date and time combined and discharge. For some reason when I do this minutes will return to 0 randomly. I am not sure how to fix this so my plot will look normal. The file is too large I cannot import it so I attcahed a screenshot of the original data table and of T. Thanks in advance!!
dataTable = readtable(csvFilePath);
Date=datetime(dataTable{:,16})+minutes(dataTable{:,9});
Discharge=dataTable(:,10);
Q =[Discharge]
Hour=dataTable(:,9)
T_cell = table2array(Q);
Hour_cell=table2array(Hour);
times = datenum(Hour_cell);
t = table(Date,times);
dates = datetime(t.Date,'Format','MM/dd/yyyy HH:mm:SS');
times = datetime(t.times,'ConvertFrom','datenum','Format','MM/dd/yyyy HH:mm:SS');
fullt = dates+timeofday(times);
t.DatesNTimes = fullt;
T=table(fullt, T_cell);
3 comentarios
Les Beckham
el 14 de Nov. de 2023
Can you post a subset of the "too large" dataset that illustrates the issue?
Respuesta aceptada
Cris LaPierre
el 15 de Nov. de 2023
Editada: Cris LaPierre
el 15 de Nov. de 2023
One issue is that your RecHour variable is not minutes. It is time of day in military time format (no colon. Should be 4 numbers, but that's likely lost due to converstion to numeric data type). The point is it is not appropriate to use minute(dataTable{:,9}). This is why your results in row 6 are now on a different day than the original data indicates.
Also, readtable has already converted date to a datetime. No need to do this a second time.
times is created ultimately from RecHours. There is no date information here, so it doesn't make sense to use datetime to create times. It addes a meaningless date to the times. Why do this? You end up ignoring the date anyway.
Finally, you come full circle and create fullt, which appears to just be a recreation of what your 2nd line of code creates, but with a lot of intrigue and mystery behind it.
I don't see any data in 5 minute increments, so can't observe what may be happening there.
Try this updated code and let us know if the issue persists.
% Load csv file (created using Excel's Import from Image feature)
dataTable = readtable('discharge.csv');
% combine date and time to new table variable
dataTable.RecHour = hours(dataTable.RecHour/100);
dataTable.Date=dataTable.date+dataTable.RecHour
dataTable.Date.Format = 'MM/dd/yyyy HH:mm:ss';
% Extract the desired variables to a new table
T = dataTable(:,["Date","Discharge"])
7 comentarios
Peter Perkins
el 16 de Nov. de 2023
Chris has certainly cracked that nut, but I'm gonna suggest that you use a timetable, not just a table.
Más respuestas (0)
Ver también
Categorías
Más información sobre Data Import from MATLAB en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!