Having trouble accesing data through an API

1 view (last 30 days)
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

Accepted Answer

Manan Jain
Manan Jain on 21 Jul 2023
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

More Answers (0)

Categories

Find more on Develop Apps Using App Designer in Help Center and File Exchange

Products


Release

R2022b

Community Treasure Hunt

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

Start Hunting!