Calculate standard deviation with different time interval

5 views (last 30 days)
Good day, everyone.
Dummy_Raw.xlsx is a spreadsheet that contains data for each minute of a 24-hour period. I would like to calculate the standard deviation of Data_C for every five minutes. I realised I should use a loop, however it's a little tricky because my data is in time format and in excel files. The end result is expected to be as in Dummy Expected.xlsx. The empty value in the excel files will be considered as null or 0. Meaning that at the end, it will be: 288×3 table (refer below).
I'm hoping the community can assist me with this because I usually do it manually and it takes a long time.
DAY TIME Data_Exp
___ _____ ______
1 00:00 NaN
1 00:05 NaN
1 00:10 NaN
1 00:15 NaN
1 00:20 NaN
...
1 17:25 0.010648
1 17:30 0.010981
1 17:35 NaN
1 17:40 0.013079
...
1 23:50 NaN
1 23:55 NaN
  3 Comments
Walter Roberson
Walter Roberson on 15 Feb 2022
Have you tried converting to a timetable() object and using retime() ?
Ann
Ann on 17 Feb 2022
Edited: Ann on 17 Feb 2022
Hi Dyuman, I did not make use a loop.
Hello Walter, I did use retime but it's possible that my original spreadsheet file is too large for MATLAB to run, as it stated "array exceeds maximum array size preference".
I'm jammed on making them in 288 minutes (1440 minutes divided 5 minutes) per day for 365 days.. Meaning 288 minutes (per day) x 365 days. Then I can't continue to calculate the standard deviation also.
% Load the data
data = readtable("PRN1_365.xlsx");
% Create a datetime
data.TimeStamp = datetime(2014,01,01) + data.DAY-1 + data.TIME;
data.TimeStamp.Format = "MM-dd-yy HH:mm";
% Convert the table to a timetable
dataTT = table2timetable(data,"RowTimes","TimeStamp");
% Retime for everyday with 5 minutes in 24 hrs
PRNTT5min = retime(dataTT(:,"VTEC"),"regular","mean","TimeStep",minutes(5));

Sign in to comment.

Accepted Answer

Walter Roberson
Walter Roberson on 17 Feb 2022
Here is a considerable improvement in performance:
Load the data
filename = "PRN1_365.xlsx";
opt = detectImportOptions(filename);
opt = setvartype(opt, "DAY", "double");
opt = setvartype(opt, "TIME", "duration");
opt = setvartype(opt, "TEC0", "double");
opt.SelectedVariableNames = ["DAY", "TIME", "TEC0"];
data = readtable(filename, opt);
% Create a datetime
data.TimeStamp = datetime(2014,01,01) + data.DAY-1 + data.TIME;
data.TimeStamp.Format = "MM-dd-yy HH:mm";
tic
% Convert the table to a timetable
dataTT = table2timetable(data(:,["TimeStamp", "TEC0"]),"RowTimes","TimeStamp");
toc
tic
% Retime for everyday with 5 minutes in 24 hrs
PRNTT5min = retime(dataTT(:,"TEC0"),"regular","mean","TimeStep",minutes(5));
toc
On my system, this executes in a fairly small number of seconds and does not run out of memory.
By contrast, before I added in the detectImportOptions, the code took several minutes and then MATLAB would abruptly quit -- probably ran out of memory. Because of all the empty rows of TEC0 data, MATLAB's default detection is that most columns are character.
  3 Comments
Walter Roberson
Walter Roberson on 27 Feb 2022
Edited: Walter Roberson on 27 Feb 2022
Ah. At the location of 'mean' in the retime() command, replace the 'mean' with a handle to a function
@(x)std(x,[],1,'omitnan')
Note that for blocks that have no data in the time span, the result will be nan. Blocks that only have nan data within the time span will also generate nan. Blocks that have exactly one finite numeric reading will generate 0 no matter what the reading is. You will only get a nonzero result if there are multiple finite entries in a time slot (and, of course, they are not all the same)
Ann
Ann on 27 Feb 2022
Many thanks, Walter. It functions as expected. I also make manual calculations to confirm that everything is calculated accurately and that the answer is as expected. Thank you so much and have a great day ahead.

Sign in to comment.

More Answers (0)

Products


Release

R2020b

Community Treasure Hunt

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

Start Hunting!