How to convert 30s daily data into hourly data using MATLAB?
8 次查看(过去 30 天)
显示 更早的评论
I have a daily 30s file.I need to convert it into hourly basis and plot the data on hourly basis. The file consists of 5 coloumns and 2858 rows. First coloumn is the time in second, second column is the second to hour conversion and all other columns are the error measurements(3,4,5).I have to plot each column data(3,4,5) in 24 hour duration with a time scale of 4hrs.I'm using old version of MATLAB.How can I combine these values and plot this.Can anyone please suggest me a solution.
2 个评论
Dyuman Joshi
2024-1-2
"First coloumn is the time in second, second column is the second to hour conversion ..."
How does the conversion work? Just multiply times in seconds with seconds to hour conversion?
What would be the context of the data obtained?
"I have to plot each column data(3,4,5) in 24 hour duration with a time scale of 4hrs"
What do you mean by this?
How is the data obtained related to this?
采纳的回答
Star Strider
2024-1-2
It would be nice to know what version you are using. Are you supposed to accumulate (for example take the mean) of the data over 4 hours, or just plot the data at 4-hour intervals, or something else?
T1 = readtable('sample_hourly_...estdata_E.csv')
Time = datetime([2024 01 01]) + seconds(T1{:,1});
T2 = [table(Time) T1(:,3:5)];
T2.Time.Format = 'HH:mm:ss'
VN = T2.Properties.VariableNames;
q4h1 = (rem(hour(T2.Time), 4) == 0) & (minute(T2.Time) == 0) & (second(T2.Time) == 0);
T2(q4h1,:)
% nnz(q4h)
figure
plot(T2{q4h1,1}, T2{q4h1,2:end})
grid
xlabel('Time')
ylabel('Value')
title('Every 4 Hour Values')
legend(VN{2:end}, 'Location','best')
q4h2 = (rem(hour(T2.Time), 4) == 0) & (minute(T2.Time) == 0) & (second(T2.Time) == 30);
T2(q4h2,:)
% nnz(q4h)
figure
plot(T2{q4h2,1}, T2{q4h2,2:end})
grid
xlabel('Time')
ylabel('Value')
title('Every 4 Hour Values + 30 Seconds')
legend(VN{2:end}, 'Location','best')
TT2 = table2timetable(T2);
TT2.Time.Format = 'HH:mm:ss';
TT2r = retime(TT2, 'regular','mean','TimeStep',hours(4))
figure
plot(TT2r.Time, TT2r{:,1:end})
grid
xlabel('Time')
ylabel('Value')
title('Meaned 4 Hour Values (‘retime’)')
legend(VN{2:end}, 'Location','best')
.
4 个评论
Star Strider
2024-1-30
My pleasure!
If my Answer helped you solve your problem, please Accept it!
In R2015b, you problably need to use ( 'PreserveVariableNames',true ). I believe that changed in R2020a (although I am not certain about the exact version).
.
更多回答(2 个)
Dinesh
2024-1-2
Hi Aiswarya,
To plot your data on an hourly basis for each error column, you'll need to group the 30 second interval data into hourly averages and then plot each error measurement. The following MATLAB snippet demonstrates how you might average one of the error measurements (3rd column from your data) over each hour. Repeat the process for the other error columns as well.
% Load "data" from your CSV file
timeHours = ceil(data(:, 2)); % Convert to hourly
error1HourlyAvg = accumarray(timeHours, data(:, 3), [], @mean); % Averages for error 1
plot(error1HourlyAvg);
The following link is the documenation for accumarray:
Alexander
2024-1-2
Hi Aiswarya,
I also can only assume your intention. My solution would be:
clear
data = dlmread('sample_hourly_shlg_testdata_E.txt',',');
plot(data(:,2), data(:,3:5));grid minor;
xticks([0 4 8 12 16 20 24]); % scale to 4 hours
xlabel('Time [h]');ylabel('your dimension');title('Your Title')
legend('M1','M2','M3');
Hopefully it helps.
3 个评论
Alexander
2024-1-4
Just for my understanding, what means "not working". Do you get an error message? Or does the code didn't meet you expectations?
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Dates and Time 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!