Borrar filtros
Borrar filtros

Having trouble accesing data through an API

2 visualizaciones (últimos 30 días)
Aaron
Aaron el 21 de Jul. de 2023
Respondida: Manan Jain el 21 de Jul. de 2023
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

Respuesta aceptada

Manan Jain
Manan Jain el 21 de Jul. de 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

Más respuestas (0)

Categorías

Más información sobre Develop Apps Using App Designer en Help Center y File Exchange.

Productos


Versión

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by