Having trouble accesing data through an API
1 次查看(过去 30 天)
显示 更早的评论
Building an app in Matlabs app desgin. Im want to have the code find the country, selected by the user, and populate information about the country. I kep recieving this error about dot indexing.
app.data = webread('https://restcountries.com/v3.1/all');
app.Country_Select.Value = 'Jordan'
selectedCountry = app.Country_Select.Value;
% Find the index of the selected country in the data
n = find(strcmp(app.data.name.common, selectedCountry));
if isempty(n)
% Country not found in data, handle the error gracefully
app.clearUIFields();
return;
end
0 个评论
采纳的回答
Manan Jain
2023-7-21
Hi!
The error you are encountering is related to dot indexing not being supported for variables of type cell. The data you obtained from the web request is stored as a cell array, and you are trying to access its contents using dot indexing, which is not allowed for cell arrays in MATLAB.
To fix this issue, you can use curly braces {} instead of dot . to access the contents of a cell array.
Here is the modified snippet of code that you can refer:
app.data = webread('https://restcountries.com/v3.1/all');
app.Country_Select.Value = 'Jordan';
app = struct with fields:
data: {250×1 cell}
Country_Select: [1×1 struct]
selectedCountry = app.Country_Select.Value;
% Find index of the selected country in the data
n = find(strcmp(app.data{1}.name.common, selectedCountry));
if isempty(n)
% Country not found in data, handle the error gracefully
app.clearUIFields();
return;
end
Note that app.data{1} accesses the first cell of the cell array, and then name.common accesses the 'name' field of the country data. Adjust the indexing as needed depending on the structure of the data you retrieved from the API.
I hope this helps!
Thanks
0 个评论
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Develop Apps Using App Designer 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!