Dot indexing is not supported for variables of this type.

조회 수: 3 (최근 30일)
Yunus Emre
Yunus Emre 2024년 5월 25일
댓글: Image Analyst 2024년 5월 25일
Dot indexing is not supported for variables of this type.
Error in fetchFiveDayForecast (line 18)
dt(i) = datetime(list(i).dt_txt, 'InputFormat', 'yyyy-MM-dd HH:mm:ss', 'Format', 'yyyy-MM-dd HH:mm:ss', 'TimeZone', 'UTC');
Error in app2/updateWeatherData (line 27)
weatherData = fetchFiveDayForecast(selectedCity, app.apiKey);
Error in app2/GetInfoButtonPushed (line 62)
app.updateWeatherData();
Error in matlab.apps.AppBase>@(source,event)executeCallback(ams,app,callback,requiresEventData,event) (line 62)
newCallback = @(source, event)executeCallback(ams, ...
Error while evaluating Button PrivateButtonPushedFcn.
Those are the error I got.
function weatherData = fetchFiveDayForecast(city,apiKey)
baseUrl = 'https://api.openweathermap.org/data/2.5/forecast';
url = sprintf('%s?q=%s&appid=%s&units=metric', baseUrl, city, apiKey);
response = webread(url);
% Process JSON data
list = response.list;
% Preallocate arrays for efficiency
numEntries = length(list);
dt = datetime.empty(numEntries, 0);
temp = zeros(numEntries, 1);
humidity = zeros(numEntries, 1);
windSpeed = zeros(numEntries, 1);
% Loop through each entry in the list and extract data
for i = 1:numEntries
dt(i) = datetime(list(i).dt_txt, 'InputFormat', 'yyyy-MM-dd HH:mm:ss', 'Format', 'yyyy-MM-dd HH:mm:ss', 'TimeZone', 'UTC');
temp(i) = list(i).main.temp;
humidity(i) = list(i).main.humidity;
windSpeed(i) = list(i).wind.speed;
end
% Store the data in the output structure
weatherData.dt = dt;
weatherData.temp = temp;
weatherData.humidity = humidity;
weatherData.windSpeed = windSpeed;
end
properties (Access = private)
apiKey = '8837203c2a21882aa541f6d408e564ab'; % Gerçek API anahtarınız ile değiştirin
cities = {'Adana' , 'Ankara' , 'İstanbul' , 'Antalya'}; % Şehirler için boş bir hücre dizisi başlatın
end
methods (Access = private)
function updateWeatherData(app,~)
selectedCity = app.CityDropDown.Value;
weatherData = fetchFiveDayForecast(selectedCity, app.apiKey);
% Update Temperature Plot
plot(app.UIAxes, weatherData.dt, weatherData.temp, '-o');
app.UIAxes.Title.String = 'Temperature (°C)';
% Update Humidity Plot
plot(app.UIAxes2, weatherData.dt, weatherData.humidity, '-o');
app.UIAxes2.Title.String = 'Humidity (%)';
% Update Wind Speed Plot
plot(app.UIAxes3, weatherData.dt, weatherData.windSpeed, '-o');
app.UIAxes3.Title.String = 'Wind Speed (m/s)';
end
function addCity(app)
newCity = app.CityEditField.Value;
if ~any(strcmp(app.cities, newCity))
app.cities{end+1} = newCity;
app.CityDropDown.Items = app.cities;
end
end
end
function AddCityButtonPushed(app, event)
app.addCity();
end
function GetInfoButtonPushed(app, event)
app.updateWeatherData();
end

채택된 답변

Torsten
Torsten 2024년 5월 25일
이동: Torsten 2024년 5월 25일
Test before your loop
% Loop through each entry in the list and extract data
for i = 1:numEntries
dt(i) = datetime(list(i).dt_txt, 'InputFormat', 'yyyy-MM-dd HH:mm:ss', 'Format', 'yyyy-MM-dd HH:mm:ss', 'TimeZone', 'UTC');
temp(i) = list(i).main.temp;
humidity(i) = list(i).main.humidity;
windSpeed(i) = list(i).wind.speed;
end
what you get when you try to access
list(1).dt_txt
list(1).main.temp
list(1).main.humidity
list(1).wind.speed
I doubt that dot indexing is allowed for "list".
  댓글 수: 1
Image Analyst
Image Analyst 2024년 5월 25일
After youi create list, do this
% Process JSON data
list = response.list % No semicolon
whos list % Display info about list.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Introduction to Installation and Licensing에 대해 자세히 알아보기

태그

제품


릴리스

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by