How can i resloved this problem ?

I am trying to ged weather from web sie... and i have this issue, when i run the cicle: Index exceeds matrix dimensions.
I use this code :
close all
clear all
%%download weather forecast data from server
current_files=urlread('https://freemeteo.bg/weather/sofia/daily-forecast/today/?gid=727011&language=bulgarian&country=bulgaria');
relev_files=strfind(current_files,'FPTO51.0_r1101_TA.csv');
%for most recent file use last index in relev_files
ta_url=['https://freemeteo.bg/weather/sofia/daily-forecast/today/', ...
'?gid=727011&language=bulgarian&country=bulgaria', ...
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:length(D)
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,2));
J = strsplit(H, ':');
hour(I) = str2double(char(J(1,1)));
min(I) = str2double(char(J(1,2)));
K = char(J(1,3));
L = strsplit(K, 'Z');
sec(I) = str2double(char(L(1,1)));
M = char(L(1,2));
N = strsplit(M, ',');
value(I) = str2double(char(N(1,2)));
end
F = strsplit(D(1,:), 'T')
data=[year' month' day' hour' min' sec' value'];
figure
t = datetime(year, month, day, hour, min, sec, value);
plot(t, value, '*')
hold on
plot(t, value, '-k', 'Linewidth', 2)
axis tight, grid minor
xlabel('Time')
ylabel('Temperature (°C)')

3 个评论

Please post the complete error message, such that the readers do not have to guess in which line the problem occurs.
This is the complete error:Index exceeds matrix dimensions.
Actually, NO. That is not the complete text of the error message. There will be more text, all in red.

请先登录,再进行评论。

 采纳的回答

The lack of comments, the unnecessary brackets and the meaningless variable names make your code harder to read than it should be.
C = [strsplit(ta_data, ('\n'))]';
without the unnecessary cruft is:
C = strsplit(ta_data, '\n')'
Anyway, length strikes again! It's the cause of your error. Forget about the length function, wipe it from your mind since you don't know how it works. If you want to know the length of a vector use numel instead. It works the same for vectors.
If you want to know the number of rows or columns of a matrix, always use size with an explicit dimension. length will return either, whichever is greater. If you test your code with a matrix that have more rows than columns, it works fine. Suddenly, you use it on a matrix with more columns than rows and all hell breaks loose (matlab errors).
for I = 1:size(D, 1)
would fix the error (assuming that there aren't more errors in your code). Although it's unclear why you don't iterate over the elements of the cell array instead of bothering to convert to char:
for line = 1:numel(C)
E = strsplit(C{line}, '-'); %how about using a more imaginative name than E? linesegment would explain the intent better
%...
end

3 个评论

Lines 1, 2, and 3 of the data returned by the url read do not have any '-' characters.
Lines 4 and 5 have '-' characters because they have the phrase 'http-equiv'. No years are there.
The current year does not exist in any entry until #1896.
@Tsvetan Terziev: You run this code in the command window? Why? Simply store it in a script or function and you can use the debugger to examine the code during it runs. Then the error message contains a line number also.
By the way, use curly braces to access elements of a cell string:
% H = char(G(1,2))
H = G{1, 2}; % Faster and nicer

请先登录,再进行评论。

更多回答(1 个)

close all
clear all
%%download weather forecast data from server
current_files=urlread('https://freemeteo.bg/weather/sofia/daily-forecast/today/?gid=727011&language=bulgarian&country=bulgaria');
relev_files=strfind(current_files,'FPTO51.0_r1101_TA.csv');
%for most recent file use last index in relev_files
ta_url=['https://freemeteo.bg/weather/sofia/daily-forecast/today/?gid=727011&language=bulgarian&country=bulgaria',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)')
why would i be getting NaN drom a matrix?

类别

帮助中心File Exchange 中查找有关 Weather and Atmospheric Science 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by