Reading a dat file; there is a bug

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 日

0 投票

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 日
so it has no error? or did you correct anything??
KALYAN ACHARJYA
KALYAN ACHARJYA 2018 年 12 月 20 日
編集済み: KALYAN ACHARJYA 2018 年 12 月 20 日
Just changed the extention name dat to txt, as you provides file have txt extention.
I hope the question is answered, if you have any doubt, let me know here.
GEORGIOS BEKAS
GEORGIOS BEKAS 2018 年 12 月 20 日
error: gb: =: nonconformant arguments (op1 is 1x1, op2 is 1x0)
error: called from
gb at line 20 column 15
KALYAN ACHARJYA
KALYAN ACHARJYA 2018 年 12 月 20 日
Do the steps:
  1. Just click on the attach file (you provided), copy all
  2. Open word pad and paste there
  3. Save as filename.txt (Save it i Desktop)
  4. Copy the code (I have provided)
  5. Run it with new file path (Comple path, Right Click properties along with slash file name)
Star Strider
Star Strider 2018 年 12 月 20 日
@Georgios Bekas —
The error you posted appears to have been issued by Octave. Octave and MATLAB are similar, however they are not the same in their function executions. You may have to experiment with Kalyan Acharjya’s code to make it work in Octave.
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.

サインインしてコメントする。

カテゴリ

製品

リリース

R2018a

質問済み:

2018 年 12 月 20 日

コメント済み:

2018 年 12 月 20 日

Community Treasure Hunt

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

Start Hunting!

Translated by