Help with a For Loop?

조회 수: 2 (최근 30일)
John Evans
John Evans 2018년 4월 27일
답변: John Evans 2018년 5월 8일
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.

채택된 답변

Walter Roberson
Walter Roberson 2018년 4월 27일
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
John Evans
John Evans 2018년 5월 7일
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 2018년 5월 7일
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.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

John Evans
John Evans 2018년 5월 8일
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

카테고리

Help CenterFile Exchange에서 Characters and Strings에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by