Why i am getting Nan?

1 回表示 (過去 30 日間)
Tsvetan Terziev
Tsvetan Terziev 2019 年 5 月 7 日
回答済み: Walter Roberson 2019 年 5 月 7 日
I'm trying to download the weather forecast from a site.
close all
clear all
%%download weather forecast data from server
current_files=urlread('https://www.sinoptik.bg/sofia-bulgaria-100727011?location');
relev_files=strfind(current_files,'FPTO51.0_r1101_TA.csv');
%for most recent file use last index in relev_files
ta_url=['https://www.sinoptik.bg/sofia-bulgaria-100727011?location',current_files(relev_files(1:end)-21:relev_files(1:end)+20)];
% ta_url=['http://dd.weatheroffice.ec.gc.ca/meteocode/ont/csv/',current_files(relev_files(1:end)-21:relev_files(1:end)+20)];
ta_data=urlread(ta_url);
%%put forecast data into vectors
clc
C = [strsplit(ta_data, '\n')]' ;
D = char(C(2:end-1));
for I = 1:numel(D,1)
E = strsplit(D(I,:));
year(I)=str2double(char(E(1,1)));
month(I) = str2double(char(E(1,2)));
F =char( E(1,3));
G = strsplit(F, 'T');
day(I) = str2double(char(G(1,1)));
H = char(G(1,1));
J = strsplit(H, ':');
hour(I) = str2double(char(J(1,1)));
min(I) = str2double(char(J(1,2)));
K = J{1,1};
L = strsplit(K, 'Z');
sec(I) = str2double(char(L(1,1)));
M = L{1,1};
N = strsplit(M, ',');
value(I) = str2double(char(N(1,1)));
end
%F = strsplit(D(1,:), 'T')
data=[year' month' day' hour' min' sec' value'];
figure
t = datetime(year, month, day, hour, min, sec);
plot(t, value, '*')
hold on
plot(t, value, '-k', 'Linewidth', 2)
axis tight, grid minor
xlabel('Time')
ylabel('Temperature (°C)')
nan.png
  1 件のコメント
Adam
Adam 2019 年 5 月 7 日
Getting NaN where? You create about 24 variables in that code.

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

回答 (2 件)

Cam Salzberger
Cam Salzberger 2019 年 5 月 7 日
編集済み: Cam Salzberger 2019 年 5 月 7 日
Any time str2double is unable to translate a particular input into a number, it returns NaN. That is probably where the issue is occuring, so consider checking the inputs more carefully. Also, numel generally takes just one input. I think you meant to use size(D, 1).
-Cam

Walter Roberson
Walter Roberson 2019 年 5 月 7 日
There is no year in any line until after line 1800.

カテゴリ

Help Center および File ExchangeWeather and Atmospheric Science についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by