Reading a dat file; there is a bug
Afficher commentaires plus anciens
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?
Réponses (1)
KALYAN ACHARJYA
le 20 Déc 2018
Modifié(e) : KALYAN ACHARJYA
le 20 Déc 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)

7 commentaires
GEORGIOS BEKAS
le 20 Déc 2018
KALYAN ACHARJYA
le 20 Déc 2018
Modifié(e) : KALYAN ACHARJYA
le 20 Déc 2018
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
le 20 Déc 2018
KALYAN ACHARJYA
le 20 Déc 2018
Do the steps:
- Just click on the attach file (you provided), copy all
- Open word pad and paste there
- Save as filename.txt (Save it i Desktop)
- Copy the code (I have provided)
- Run it with new file path (Comple path, Right Click properties along with slash file name)
Star Strider
le 20 Déc 2018
@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
le 20 Déc 2018
GEORGIOS BEKAS
le 20 Déc 2018
Catégories
En savoir plus sur Octave dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!