How to convert 30s daily data into hourly data using MATLAB?
조회 수: 6 (최근 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.
참고 항목
카테고리
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!