Having trouble accesing data through an API

2 次查看(过去 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'
app = struct with fields:
data: {250×1 cell} Country_Select: [1×1 struct]
selectedCountry = app.Country_Select.Value;
% Find the index of the selected country in the data
n = find(strcmp(app.data.name.common, selectedCountry));
Dot indexing is not supported for variables of this type.
if isempty(n)
% Country not found in data, handle the error gracefully
app.clearUIFields();
return;
end

采纳的回答

Manan Jain
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 个)

类别

Help CenterFile Exchange 中查找有关 Develop Apps Using App Designer 的更多信息

产品


版本

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by