Reading a dat file; there is a bug

조회 수: 1 (최근 30일)
GEORGIOS BEKAS
GEORGIOS BEKAS 2018년 12월 20일
댓글: GEORGIOS BEKAS 2018년 12월 20일
I have written the following code in order for MATLAB to read a dat file that I attach.
There is a bug in line 20, despite the fact that the code is OK.
A = importdata ('C:\Users\user\Desktop\O3#Thr2017.dat', ',');
A.data;
A.data(A.data== -9999) = NaN;
Z = reshape(A.data,[365,24]);
yearlyavg=mean(Z(~isnan(Z)));
hourlyavg = zeros(1,24);
for i = 1:size(Z,2)
h = Z(:,i);
hourlyavg(i) = mean(h(~isnan(h)));
end
dailyavg = zeros(1,365);
for i = 1:size(Z,1)
g = Z(i,:);
dailyavg(i) = mean(g(~isnan(g)));
end
months = [31 28 31 30 31 30 31 31 30 31 30 31];
j = cumsum(months)
monthlyavg = zeros(1,12);
January = Z(1:31,:);
February = Z(j(1)+1:j(2),:);
March = Z(j(2)+1:j(3),:);
April = Z(j(3)+1:j(4),:);
May = Z(j(4)+1:j(5),:);
June = Z(j(5)+1:j(6),:);
July = Z(j(6)+1:j(7),:);
August = Z(j(7)+1:j(8),:);
September = Z(j(8)+1:j(9),:);
October = Z(j(9)+1:j(10),:);
November = Z(j(10)+1:j(11),:);
December = Z(j(11)+1:365,:);
monthlyavg(1) = mean(January(~isnan(January)));
monthlyavg(2) = mean(February(~isnan(February)));
monthlyavg(3) = mean(March(~isnan(March)));
monthlyavg(4) = mean(April(~isnan(April)));
monthlyavg(5) = mean(May(~isnan(May)));
monthlyavg(6) = mean(June(~isnan(June)));
monthlyavg(7) = mean(July(~isnan(July)));
monthlyavg(8) = mean(August(~isnan(August)));
monthlyavg(9) = mean(September(~isnan(September)));
monthlyavg(10) = mean(October(~isnan(October)));
monthlyavg(11) = mean(November(~isnan(November)));
monthlyavg(12) = mean(December(~isnan(December)));
months= 1:12;
days = 1:365;
hours = 1:24;
plot (days,dailyavg)
plot (months,monthlyavg)
plot (hours,hourlyavg)
Can you help me?

답변 (1개)

KALYAN ACHARJYA
KALYAN ACHARJYA 2018년 12월 20일
편집: KALYAN ACHARJYA 2018년 12월 20일
No error here check it. extention .txt
A=importdata('C:\Users\sony\Desktop\test.txt', ',');
A.data;
A.data(A.data== -9999) = NaN;
Z = reshape(A.data,[365,24]);
yearlyavg=mean(Z(~isnan(Z)));
hourlyavg = zeros(1,24);
for i = 1:size(Z,2)
h = Z(:,i);
hourlyavg(i) = mean(h(~isnan(h)));
end
dailyavg = zeros(1,365);
for i = 1:size(Z,1)
g = Z(i,:);
dailyavg(i) = mean(g(~isnan(g)));
end
months = [31 28 31 30 31 30 31 31 30 31 30 31];
j = cumsum(months)
monthlyavg = zeros(1,12);
January = Z(1:31,:);
February = Z(j(1)+1:j(2),:);
March = Z(j(2)+1:j(3),:);
April = Z(j(3)+1:j(4),:);
May = Z(j(4)+1:j(5),:);
June = Z(j(5)+1:j(6),:);
July = Z(j(6)+1:j(7),:);
August = Z(j(7)+1:j(8),:);
September = Z(j(8)+1:j(9),:);
October = Z(j(9)+1:j(10),:);
November = Z(j(10)+1:j(11),:);
December = Z(j(11)+1:365,:);
monthlyavg(1) = mean(January(~isnan(January)));
monthlyavg(2) = mean(February(~isnan(February)));
monthlyavg(3) = mean(March(~isnan(March)));
monthlyavg(4) = mean(April(~isnan(April)));
monthlyavg(5) = mean(May(~isnan(May)));
monthlyavg(6) = mean(June(~isnan(June)));
monthlyavg(7) = mean(July(~isnan(July)));
monthlyavg(8) = mean(August(~isnan(August)));
monthlyavg(9) = mean(September(~isnan(September)));
monthlyavg(10) = mean(October(~isnan(October)));
monthlyavg(11) = mean(November(~isnan(November)));
monthlyavg(12) = mean(December(~isnan(December)));
months= 1:12;
days = 1:365;
hours = 1:24;
plot (days,dailyavg)
plot (months,monthlyavg)
plot (hours,hourlyavg)
jj.png
  댓글 수: 7
GEORGIOS BEKAS
GEORGIOS BEKAS 2018년 12월 20일
yes. I have found the error.
There is also another bug.
The mean of a matrix A is expressed as mean(mean(A)).
GEORGIOS BEKAS
GEORGIOS BEKAS 2018년 12월 20일
We should replace the NaNs with zeros, and use the plain mean function.

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

카테고리

Help CenterFile Exchange에서 Surrogate Optimization에 대해 자세히 알아보기

제품


릴리스

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by