Dot indexing is not supported for variables of this type.
2 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Yunus Emre
am 25 Mai 2024
Kommentiert: Image Analyst
am 25 Mai 2024
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
0 Kommentare
Akzeptierte Antwort
Torsten
am 25 Mai 2024
Verschoben: Torsten
am 25 Mai 2024
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 Kommentar
Image Analyst
am 25 Mai 2024
After youi create list, do this
% Process JSON data
list = response.list % No semicolon
whos list % Display info about list.
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu JSON Format finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!