Reading a dat file; there is a bug

1 view (last 30 days)
GEORGIOS BEKAS
GEORGIOS BEKAS on 20 Dec 2018
Commented: GEORGIOS BEKAS on 20 Dec 2018
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?

Answers (1)

KALYAN ACHARJYA
KALYAN ACHARJYA on 20 Dec 2018
Edited: KALYAN ACHARJYA on 20 Dec 2018
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 Comments
GEORGIOS BEKAS
GEORGIOS BEKAS on 20 Dec 2018
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 on 20 Dec 2018
We should replace the NaNs with zeros, and use the plain mean function.

Sign in to comment.

Products


Release

R2018a

Community Treasure Hunt

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

Start Hunting!