필터 지우기
필터 지우기

Having trouble accesing data through an API

조회 수: 2 (최근 30일)
Aaron
Aaron 2023년 7월 21일
답변: Manan Jain 2023년 7월 21일
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