How can i resloved this problem ?
12 views (last 30 days)
Show older comments
Tsvetan Terziev
on 2 May 2019
Answered: Tsvetan Terziev
on 5 May 2019
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 Comments
John D'Errico
on 2 May 2019
Actually, NO. That is not the complete text of the error message. There will be more text, all in red.
Accepted Answer
Guillaume
on 2 May 2019
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 Comments
Walter Roberson
on 2 May 2019
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.
Jan
on 3 May 2019
@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
More Answers (1)
See Also
Categories
Find more on Weather and Atmospheric Science in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!