Array indices must be positive integers or logical values. Error in Untitled2 (line 11) '_', Station_ids (i),...
2 次查看(过去 30 天)
显示 更早的评论
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 个评论
Walter Roberson
2021-10-25
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)
回答(1 个)
Siraj
2024-2-21
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)
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
For more information on the “webread” function, you can visit the following link
Hope this helps.
0 个评论
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!