How to convert 30s daily data into hourly data using MATLAB?

5 views (last 30 days)
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 Comments
Dyuman Joshi
Dyuman Joshi on 2 Jan 2024
"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?
Aiswarya
Aiswarya on 3 Jan 2024
Thank you for the response. I changed the data time format to hh:mm:ss. The latest csv file consist for time in first column and the corresponding errors in the next columns. I have to plot the 30s data series on hourly basis in 4hrs interval and average the data for hours and plot it.

Sign in to comment.

Accepted Answer

Star Strider
Star Strider on 2 Jan 2024
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?
This is straightforward using a timetable and the retime function —
T1 = readtable('sample_hourly_...estdata_E.csv')
T1 = 2858×5 table
Var1 Var2 Var3 Var4 Var5 ____ _________ ______ _______ ______ 30 0.0083333 5.9288 0.96482 4.913 60 0.016667 6.2877 0.97484 4.7815 90 0.025 5.9988 1.06 4.8505 120 0.033333 5.6999 1.0159 5.6315 150 0.041667 5.7833 1.0239 4.7822 180 0.05 6.0166 0.75248 4.7387 210 0.058333 5.9688 0.97885 4.8818 240 0.066667 6.0499 0.87468 4.9862 270 0.075 5.7744 1.2132 4.3773 300 0.083333 5.541 0.96182 4.9297 330 0.091667 5.871 0.89271 5.0361 360 0.1 5.8955 1.2292 4.8141 390 0.10833 5.5332 1.3695 5.2932 420 0.11667 6.1322 1.1832 5.1075 450 0.125 6.5078 0.99988 3.9719 480 0.13333 5.7188 1.1411 4.9198
Time = datetime([2024 01 01]) + seconds(T1{:,1});
T2 = [table(Time) T1(:,3:5)];
T2.Time.Format = 'HH:mm:ss'
T2 = 2858×4 table
Time Var3 Var4 Var5 ________ ______ _______ ______ 00:00:30 5.9288 0.96482 4.913 00:01:00 6.2877 0.97484 4.7815 00:01:30 5.9988 1.06 4.8505 00:02:00 5.6999 1.0159 5.6315 00:02:30 5.7833 1.0239 4.7822 00:03:00 6.0166 0.75248 4.7387 00:03:30 5.9688 0.97885 4.8818 00:04:00 6.0499 0.87468 4.9862 00:04:30 5.7744 1.2132 4.3773 00:05:00 5.541 0.96182 4.9297 00:05:30 5.871 0.89271 5.0361 00:06:00 5.8955 1.2292 4.8141 00:06:30 5.5332 1.3695 5.2932 00:07:00 6.1322 1.1832 5.1075 00:07:30 6.5078 0.99988 3.9719 00:08:00 5.7188 1.1411 4.9198
VN = T2.Properties.VariableNames;
q4h1 = (rem(hour(T2.Time), 4) == 0) & (minute(T2.Time) == 0) & (second(T2.Time) == 0);
T2(q4h1,:)
ans = 5×4 table
Time Var3 Var4 Var5 ________ _______ ________ _______ 04:00:00 2.0807 1.8583 -1.0893 08:00:00 -3.8786 6.5277 -11.2 12:00:00 -6.4366 -0.82104 -7.0585 16:00:00 2.1863 -0.14896 4.6413 20:00:00 5.141 -0.4865 6.2848
% 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,:)
ans = 6×4 table
Time Var3 Var4 Var5 ________ _______ ________ _______ 00:00:30 5.9288 0.96482 4.913 04:00:30 2.2096 2.0275 -0.8845 08:00:30 -3.7675 6.6449 -11.714 12:00:30 -7.0522 -0.76595 -6.5867 16:00:30 2.143 0.28073 4.6995 20:00:30 4.6265 -0.56563 5.4408
% 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))
TT2r = 6×3 timetable
Time Var3 Var4 Var5 ________ _______ _________ _______ 00:00:00 4.6571 0.40753 3.0897 04:00:00 -2.852 2.2892 -6.5746 08:00:00 -5.5369 -1.0768 -7.5661 12:00:00 -5.6504 -1.4982 1.6178 16:00:00 3.5855 -0.10615 3.7615 20:00:00 6.0721 -0.015406 5.938
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 Comments
Aiswarya
Aiswarya on 30 Jan 2024
Dear Sir,
Sorry for the late reply. Thank you for the continuous assistance. I'm facing some issues while loading the CSV file using readtable command in Matlab2015b. But I try these codes in another version. It's working fine.
Star Strider
Star Strider on 30 Jan 2024
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).
.

Sign in to comment.

More Answers (2)

Dinesh
Dinesh on 2 Jan 2024
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
Alexander on 2 Jan 2024
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 Comments
Alexander
Alexander on 4 Jan 2024
Just for my understanding, what means "not working". Do you get an error message? Or does the code didn't meet you expectations?
Alexander
Alexander on 4 Jan 2024
Are you aware, that you are loosing about 12 min of data between 2623 and 2624?

Sign in to comment.

Categories

Find more on Dates and Time in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!