Interpolate missing hourly and daily data
9 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Hello everyone,
I need your help because i'm stuck in this step. I want to do interpolation on my data. I have 16 years data and it's arranged like this:
Col 1: Year; Col 2: Month; Col 3: Day; Col 4: Hour; Col 5: Minutes; Col 6: Second; Col 7: Data
Since my data are missing both in Col 3 (Day) and Col 4 (Hour), I tried used interp1 (hour) but no return, but if i did interp1 (day) the results return, since i want to interpolate both of missing hour data and missing day data how can i do it?
I'm new in matlab, so i hope your answer will help me a lot, thank you!
0 comentarios
Respuestas (2)
Star Strider
el 23 de Oct. de 2024
filename = "Missing_data.xlsx";
T1 = readtable(filename);
T1.Properties.VariableNames = {'Year','Month','Day','Hour','Minute','Second','Data'}
CheckMinSec = [nnz(T1.Minute) nnz(T1.Second)]
DateTime = datetime(T1{:,1:6}, Format="yyyy-MM-dd HH");
TT1 = timetable(DateTime,T1.Data);
TT1.Properties.VariableNames = {'Data'}
TT1r = retime(TT1,"hourly")
HourRowsAdded = height(TT1r) - height(TT1)
figure
tiledlayout(2,1)
nexttile
plot(TT1.DateTime, TT1.Data)
grid
nexttile
plot(TT1r.DateTime, TT1r.Data)
grid
There are no minutes or seconds in the original, so I left them out.
.
2 comentarios
Hitesh
el 23 de Oct. de 2024
Editada: Hitesh
el 23 de Oct. de 2024
You can use the“interp1”function to interpolate data at both hourly and daily intervals. The below-mentioned steps can help in achieving the same:
- Convert the Excel data into“datetime”format using the“datetime”function.
- Create a variable named“interpolationTimeRange”using the“datetime”function, which spans from January 1, 2004, to December 31, 2020, with hourly intervals, according to the data provided in the Excel file.
- Apply the“interp1”function with the formatted dates, formatted values, and the“interpolationTimeRange”to calculate interpolated values for the missing data.
Please refer to the below code and attached “Interpolated_Data.xlsx” file:
% Load the data from the Excel file
dataTable = readtable('Missing_data.xlsx');
% Create a datetime array using the specified columns
% Ensure that the column names match exactly with those in the Excel file
dateTimeArray = datetime(dataTable.Year, dataTable.Month, dataTable.Day, dataTable.Hour, dataTable.Minute, dataTable.Second);
valueArray = dataTable.Values;
% Create a new table with datetime and values
formattedData = table(dateTimeArray, valueArray, 'VariableNames', {'Time', 'Values'});
% Plot original data
plot(formattedData.Time, formattedData.Values, '*');
% Define the query time range for interpolation
interpolationTimeRange = (datetime(2004,1,1):hours(1):datetime(2020,12,31))';
% Perform interpolation using spline method
interpolatedValues = interp1(formattedData.Time, formattedData.Values, interpolationTimeRange, 'spline');
% Plot the interpolated data
hold on;
plot(interpolationTimeRange, interpolatedValues, 'r');
% Extract components from the interpolation time range
yearArray = year(interpolationTimeRange);
monthArray = month(interpolationTimeRange);
dayArray = day(interpolationTimeRange);
hourArray = hour(interpolationTimeRange);
minuteArray = minute(interpolationTimeRange);
secondArray = second(interpolationTimeRange);
% Create a new table with the extracted components and interpolated values
interpolatedData = table(yearArray, monthArray, dayArray, hourArray, minuteArray, secondArray, interpolatedValues, ...
'VariableNames', {'Year', 'Month', 'Day', 'Hour', 'Minute', 'Second', 'InterpolatedValues'});
% Write the new table to an Excel file
writetable(interpolatedData, 'Interpolated_Data.xlsx');
For more information on “interp1” function, refer to the below MATLAB documentation:
Ver también
Categorías
Más información sobre Tables 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!