I am pretty new to matlab (am a beginner) and is completely lost; hopefully someone can help me here.
I am currently working on an excel data set with 151 columns of data (I have attached a sample file). The first column has both the time and date on which the data points were collected (at 15min intervals for 8 weeks). Can someone help me with writing a code (eg a for loop) where data from only 12midnight to 6am for the weeks for each data set (column) is collected and then finds the average of it.
My sincere thanks for whoever can help.

 Akzeptierte Antwort

Walter Roberson
Walter Roberson am 27 Apr. 2018

1 Stimme

t = readtable('Book1.xls');
h = hour(t.Time);
m = minute(t.Time);
mask = h <= 5 | (h == 6 & m == 0); %hour 6 is only valid for 06:00 exactly
selected_data = t(mask,:);
means = nanmean(selected_data{:,2:end});
It was not really clear what interval the means were to be taken over.
You need the nanmean because there are nan in some of the columns.

12 Kommentare

John Evans
John Evans am 27 Apr. 2018
Thanks so much for the attempt, let me clarify a bit more.
Data set A (column B): for every day there is data I want all the data from 12 midnight to 6am collected (there are several weeks of data). Next we find the average of that(which is what I need). Then this is repeated for each of the other data columns (B to F) which are all separate data sets. So in the attached file I will get 6 average data points (one each for data set A to F)
I hope this helps in getting a working code
Walter Roberson
Walter Roberson am 27 Apr. 2018
The code I posted does what you indicate, at least for one plausible meaning of "average of that".
Is "average of that" to be taken over all of the midnight to 6am entries? Or is it to be taken for each midnight to 6am group independently ?
John Evans
John Evans am 27 Apr. 2018
Thanks again. Hope this explanation helps.
So for instance for data A(column B) "Average of that" refers to the average of all the midnight to 6am data points for it. Then the procedure repeats and finds all the midnight to 6am data points for data B (column C). And so on. So I will have one different average for all midnight to 6am data entries filtered out of each individual data set (column)
John Evans
John Evans am 27 Apr. 2018
Hi Walter,
Just to let you know, I tried to run the code and got this error. It couldnt parse the date/time which appears in this format mm/dd/yy hh:mm:ss
Warning: Variable names were modified to make them valid MATLAB identifiers. Error using datevec (line 276) Cannot parse date .
Error in hour (line 37) c = datevec(d(:));
Error in averages (line 2) h = hour(t.Time);
Can anyone else help me out. I am doing my own research but still haven't being able to get anything work
Walter Roberson
Walter Roberson am 27 Apr. 2018
Which MATLAB release are you using, and which operating system?
I tested the code on your data before I posted it.
John Evans
John Evans am 27 Apr. 2018
Oh I see. I will update to the latest release that is provided here (Matlab R2017b) and see what happens when I get home.
John Evans
John Evans am 28 Apr. 2018
Hello Walter,
I installed the latest release we have and indeed the code works. This is so awesome and so much simpler than what everyone was telling me. Thanks so much.
Although I would be so grateful if you could help me with what everyone I asked help from told me to do (am still studying how to that and obviously stuck). I was told I had to first convert the time (date & hours) to a matlab object using either (datetime or datevec) then create a for loop for the midnight to 6am data then do the averages. I Can I get some aid to write such a code (never written a for loop)- any help will be appreciated
Walter Roberson
Walter Roberson am 28 Apr. 2018
t = readtable('Book1a.xls');
G = groupsummary(t,'Time','day','mean');
>> head(G)
ans =
8×8 table
day_Time GroupCount mean_A mean_B mean_C mean_D mean_E mean_F
___________ __________ ________________ ________________ ________________ ________________ ________________ ________________
22-Jan-2018 85 3034.23335805265 5675.05640129264 1195.41110158453 3151.52941176471 2980.70588235294 3267.50588235294
23-Jan-2018 96 4584.47141742605 5308.43892324621 1123.54140214658 2389.92708333333 1433.92708333333 532.541666666667
Notice the modified variable names and notice the count that was included.
John Evans
John Evans am 28 Apr. 2018
Bearbeitet: Walter Roberson am 28 Apr. 2018
I appreciate this a lot. Will test it out on my whole data and see what each line does
It is refreshing you never used a for loop at all. Although I really wanted to see how to get the same results by creating a for loop
Thanks Walter.
John
John Evans
John Evans am 7 Mai 2018
Bearbeitet: Walter Roberson am 7 Mai 2018
Hello Walter,
do you know why when I try
t = readtable('Book1a.xlsx');
h = hour(t.Time);
m = minute(t.Time);
mask = h <= 5 | (h == 6 & m == 0); %hour 6 is only valid for 06:00 exactly
selected_data = t(mask,:);
means = nanmean(selected_data{:,2:end});
G = groupsummary(t,'Time','day','mean');
head(G)
I get this message back:
Warning: Variable names were modified to make them valid MATLAB
identifiers. The original names are saved in the
VariableDescriptions property.
Undefined function or variable 'groupsummary'
What am I doing wrong
John Evans
John Evans am 7 Mai 2018
Also when I try the original code after adding more columns (149 columns of data) to Book1a the code doesnt run and spits out this error (only 't' gives an output. h, mask, mean, etc give this response: Undefined function or variable 'h')
Error using datevec (line 217) Failed to lookup month of year. Error in hour (line 37) c = datevec(d(:)); Error in Untitled (line 5) h = hour(t.Time);
Kindly help.
Walter Roberson
Walter Roberson am 7 Mai 2018
Ah, looks like groupsummary() was added in R2018a. I will have a look a bit later to see what I can replace it with that will run in your version.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

John Evans
John Evans am 8 Mai 2018

0 Stimmen

Thank you. In addition to that can you please take a look my 2nd question(most important). ie when i run the code as is on Book1a (has 6 columns) it works, except for groupsummary. But when I run it on the actual Book1a file which has more than 150 columns it doesnt work. I get an output/answer for t but no output (rather error) for all others: ie means, h, mask, etc.
Really at my wits end

Kategorien

Mehr zu Characters and Strings finden Sie in Hilfe-Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by