Array indices must be positive integers or logical values. Error in Untitled2 (line 11) '_', Station_ids (i),...

1 Ansicht (letzte 30 Tage)
it keeps saying Unrecognized function or variable 'my_url'.
Error in LabFEECS (line 10)
gauge_data = webread(my_url);
and
Array indices must be positive integers or logical values.
Error in Untitled2 (line 11)
'_', Station_ids (i),...
and this is my code
my_url = strcat('https://', base_url, province, '/', frequency, '/', ...
province, '_', Station_ids (i), ...
'_', frequency, '_hydrometric.', file_type);
  1 Kommentar
Walter Roberson
Walter Roberson am 25 Okt. 2021
For the first part, we would need to see more of the code -- and we would also need to know how you are invoking the file.
For the second part: the code has not assigned a value to the variable i at that point, so as far as the code knows, i is still the function that returns sqrt(-1)

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Siraj
Siraj am 21 Feb. 2024
Hi!
It is my understanding that you’re trying to fetch data from a web service using MATLAB's “webread” function, which requires a correctly formatted URL as input.
You're building the URL called “my_url” by combining parts of a web address with an element from the "Station_ids" array using the “strcat” function.
The error messages that you’ve shared indicate that there are issues with how “my_url” is being created and subsequently used in the “webread” function. The key problem is that the index “i” you're using to access elements in the "Station_ids" array is not a valid index. In MATLAB, array indices need to be positive integers or logical values.
As mentioned in the first comment “i” by default takes a complex value. To confirm this execute the following in the Command Window
disp(i)
0.0000 + 1.0000i
If “i” is not a positive integer, MATLAB will not be able to use it to index into “Station_ids”, and as a result, “my_url” won't be created.
To resolve this, ensure that “i” is set to a valid index within the bounds of the “Station_ids” array before you use it to build “my_url”. Here's how you might correct the code:
% Setting up some variables for the URL construction
base_url = 'example.com';
province = 'SomeProvince';
frequency = 'daily';
file_type = 'csv';
Station_ids = [101, 202, 303]; % Example station IDs
% Ensure i is a positive integer within the bounds of Station_ids
for i = 1:length(Station_ids)
% Constructing the URL
my_url = strcat('https://', base_url, province, '/', frequency, '/', ...
province, '_', num2str(Station_ids(i)), ...
'_', frequency, '_hydrometric.', file_type);
% Now you can use my_url in webread
disp(my_url)
% gauge_data = webread(my_url);
% Process the gauge_data as needed
end
https://example.comSomeProvince/daily/SomeProvince_101_daily_hydrometric.csv https://example.comSomeProvince/daily/SomeProvince_202_daily_hydrometric.csv https://example.comSomeProvince/daily/SomeProvince_303_daily_hydrometric.csv
For more information on the “webread” function, you can visit the following link
Hope this helps.

Kategorien

Mehr zu Install Products finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by