Array indices must be positive integers or logical values "ERROR"

1 visualización (últimos 30 días)
Sakibur Rahman
Sakibur Rahman el 22 de Nov. de 2019
Comentada: Sakibur Rahman el 22 de Nov. de 2019
I keep getting this error(below) but im not sure why, anyone have any idea what is happening? Thanks
Array indices must be positive integers or logical values.
Error in DepthofWater (line 20)
my_url = strcat('https://', base_url, province, '/', frequency, '/', province, '_', station_id(i), '_', frequency, '_hydrometric.', file_type)
base_url = 'dd.weather.gc.ca/hydrometric/csv/'
province = 'ON';
frequency = 'daily';
file_type = 'csv';
station_id =['02HA014','02HC030','02HC028','02HC027']
station_name =["REDHILL CREEK AT HAMILTON",...
"ETOBICOKE CREEK BELOW QUEEN ELIZABETH HIGHWAY",...
"LITTLE ROUGE CREEK NEAR LOCUST HILL",...
"BLACK CREEK NEAR WESTON"]
for s=1:1:length(station_id)
my_url = strcat('https://', base_url, province, '/', frequency, '/', province, '_', station_id(i), '_', frequency, '_hydrometric.', file_type)
gauge_data = webread(my_url);
depth_data = gauge_data.WaterLevel_NiveauD_eau_m_.';
depth_data(find(isnan(depth_data)))=[];
% figure (s)
% %plot(depth_data)
% xlabel('historical daily water level(day)');
% ylabel('water level (m)');
% title(['daily water level at ', station_name(s), 'ID:' , station_id(s)]);
% define some arbitrary data
y = depth_data;
x = 1:1:length(y);
% calculate avg & standarddeviation
avg_y_scalar = mean(y);
avg_y_vector = avg_y_scalar * ...
ones(1,length(y));
std_y = std(y);
% data + 1 std deviation (above)
y_plus = y + std_y;
% data -1 std deviation (below)
y_minus = y -std_y;
% create a vector of the avg-std_dev that are in reverse order
j=1;
for i = length(y):-1:1
y_minus_reverse(j) = y_minus(i);
j=j+1; % increment j positive
end
% create plot
figure(s)
% plot the data andaverage
plot(x,y,'b',x,avg_y_vector);
% plot std deviation shape
patch([1:1:length(y) ...
length(y):-1:1],...
[y_plus y_minus_reverse],...
'b',...
'facealpha',0.05,...% fill colour
'edgecolor','r',...
'edgealpha',0.05) % edge colour
% Legend, Title, axis labels.
%figure (s)
%plot(depth_data)
xlabel('historical daily water level(day)');
ylabel('water level (m)');
title(['daily water level at ', station_name(s), 'ID:' , station_id(s)]);
legend('original data',...
'average values',...
'standard deviation')
s=s+1
end

Respuestas (1)

Walter Roberson
Walter Roberson el 22 de Nov. de 2019
for s=1:1:length(station_id)
my_url = strcat('https://', base_url, province, '/', frequency, '/', province, '_', station_id(i), '_', frequency, '_hydrometric.', file_type)
Your variable i has not been defined at that line, so it will have the default value in MATLAB of sqrt(-1) .
Note by the way that your s variable from for s is not being used in the loop.
  10 comentarios
Joe Vinciguerra
Joe Vinciguerra el 22 de Nov. de 2019
Editada: Joe Vinciguerra el 22 de Nov. de 2019
I took the recommendations from Walter and applied them here (plus a couple insignificant changes such as 1:length(station_id) instead of 1:1:length(station_id). It seeems the source of this latest error is resolved by using y_minus_reverse = fliplr(y_minus); as Walter recommended. This seemed to work for me.
base_url = 'dd.weather.gc.ca/hydrometric/csv/';
province = 'ON';
frequency = 'daily';
file_type = 'csv';
station_id =["02HA014";"02HC030";"02HC028";"02HC027"];
station_name =["REDHILL CREEK AT HAMILTON",...
"ETOBICOKE CREEK BELOW QUEEN ELIZABETH HIGHWAY",...
"LITTLE ROUGE CREEK NEAR LOCUST HILL",...
"BLACK CREEK NEAR WESTON"];
for s=1:length(station_id)
my_url = strcat('https://',...
base_url,...
province, '/',...
frequency, '/',...
province, '_',station_id(s), '_', frequency, '_hydrometric.', file_type);
gauge_data = webread(my_url);
depth_data = gauge_data.WaterLevel_NiveauD_eau_m_.';
depth_data(find(isnan(depth_data)))=[];
% figure (s)
% plot(depth_data)
% xlabel('historical daily water level(day)');
% ylabel('water level (m)');
% title(['daily water level at ', station_name(s), 'ID:' , station_id(s)]);
% define some arbitrary data
y = depth_data;
x = 1:length(y);
% calculate avg & standarddeviation
avg_y_scalar = mean(y);
avg_y_vector = avg_y_scalar * ...
ones(1,length(y));
std_y = std(y);
% data + 1 std deviation (above)
y_plus = y + std_y;
% data -1 std deviation (below)
y_minus = y -std_y;
% create a vector of the avg-std_dev that are in reverse order
y_minus_reverse = fliplr(y_minus);
% create plot
figure(s)
% plot the data andaverage
plot(x,y,'b',x,avg_y_vector);
% plot std deviation shape
patch([1:length(y) length(y):-1:1],...
[y_plus y_minus_reverse],...
'b',...
'facealpha',0.05,...% fill colour
'edgecolor','r',...
'edgealpha',0.05) % edge colour
% Legend, Title, axis labels.
% figure (s)
% plot(depth_data)
xlabel('historical daily water level(day)');
ylabel('water level (m)');
title(['daily water level at ', station_name(s), 'ID:' , station_id(s)]);
legend('original data',...
'average values',...
'standard deviation')
end
untitled.jpg
Sakibur Rahman
Sakibur Rahman el 22 de Nov. de 2019
Thanks a lot, I was using the fliplr incorrectly and thats why I was still getting an error. Tried your way and it works perfectly. Thanks everyone.

Iniciar sesión para comentar.

Categorías

Más información sobre Get Started with MATLAB en Help Center y File Exchange.

Etiquetas

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by