Reading a dat file; there is a bug

1 次查看(过去 30 天)
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