Dot indexing is not supported for variables of this type.

3 ビュー (過去 30 日間)
Yunus Emre
Yunus Emre 2024 年 5 月 26 日
回答済み: SACHIN KHANDELWAL 2024 年 6 月 28 日
I may have explained it wrong in my previous question. I will send you the full code and then I will throw out the errors I get. I would be very happy if you solve it.
fetchFiveDayForecast.m :
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
if isfield(response, 'list')
list = response.list;
else
error('Invalid response structure');
end
% Initialize data struct
data = struct();
numEntries = length(list);
% Loop through each entry in the list and extract data
for i = 1:numEntries
data(i).dt = datetime(list(i).dt_txt, 'InputFormat', 'yyyy-MM-dd HH:mm:ss', 'Format', 'yyyy-MM-dd HH:mm:ss', 'TimeZone', 'UTC');
data(i).temp = list(i).main.temp;
data(i).humidity = list(i).main.humidity;
data(i).windSpeed = list(i).wind.speed;
end
% Store the data in the output structure
weatherData = struct();
weatherData.dt = [data.dt];
weatherData.temp = [data.temp];
weatherData.humidity = [data.humidity];
weatherData.windSpeed = [data.windSpeed];
end
app2.mlapp :
classdef app2 < matlab.apps.AppBase
% Properties that correspond to app components
properties (Access = public)
UIFigure matlab.ui.Figure
GetInfoButton matlab.ui.control.Button
AddCityButton matlab.ui.control.Button
CityEditField matlab.ui.control.EditField
CityEditFieldLabel matlab.ui.control.Label
CityDropDown matlab.ui.control.DropDown
CityDropDownLabel matlab.ui.control.Label
UIAxes3 matlab.ui.control.UIAxes
UIAxes2 matlab.ui.control.UIAxes
UIAxes matlab.ui.control.UIAxes
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');
title(app.UIAxes, 'Temperature (°C)');
xlabel(app.UIAxes, 'Date and Time');
ylabel(app.UIAxes, 'Temperature (°C)');
% Update Humidity Plot
plot(app.UIAxes2, weatherData.dt, weatherData.humidity, '-o');
title(app.UIAxes2, 'Humidity (%)');
xlabel(app.UIAxes2, 'Date and Time');
ylabel(app.UIAxes2, 'Humidity (%)');
% Update Wind Speed Plot
plot(app.UIAxes3, weatherData.dt, weatherData.windSpeed, '-o');
title(app.UIAxes3, 'Wind Speed (m/s)');
xlabel(app.UIAxes3, 'Date and Time');
ylabel(app.UIAxes3, '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
% Callbacks that handle component events
methods (Access = private)
% Button pushed function: AddCityButton
function AddCityButtonPushed(app, event)
app.addCity();
end
% Button pushed function: GetInfoButton
function GetInfoButtonPushed(app, event)
app.updateWeatherData();
end
end
% Component initialization
methods (Access = private)
% Create UIFigure and components
function createComponents(app)
% Create UIFigure and hide until all components are created
app.UIFigure = uifigure('Visible', 'off');
app.UIFigure.Position = [100 100 1136 581];
app.UIFigure.Name = 'MATLAB App';
% Create UIAxes
app.UIAxes = uiaxes(app.UIFigure);
title(app.UIAxes, 'Title')
xlabel(app.UIAxes, 'X')
ylabel(app.UIAxes, 'Y')
zlabel(app.UIAxes, 'Z')
app.UIAxes.Position = [25 370 300 185];
% Create UIAxes2
app.UIAxes2 = uiaxes(app.UIFigure);
title(app.UIAxes2, 'Title')
xlabel(app.UIAxes2, 'X')
ylabel(app.UIAxes2, 'Y')
zlabel(app.UIAxes2, 'Z')
app.UIAxes2.Position = [403 370 300 185];
% Create UIAxes3
app.UIAxes3 = uiaxes(app.UIFigure);
title(app.UIAxes3, 'Title')
xlabel(app.UIAxes3, 'X')
ylabel(app.UIAxes3, 'Y')
zlabel(app.UIAxes3, 'Z')
app.UIAxes3.Position = [790 370 300 185];
% Create CityDropDownLabel
app.CityDropDownLabel = uilabel(app.UIFigure);
app.CityDropDownLabel.HorizontalAlignment = 'right';
app.CityDropDownLabel.Position = [678 231 89 22];
app.CityDropDownLabel.Text = 'City Drop Down';
% Create CityDropDown
app.CityDropDown = uidropdown(app.UIFigure);
app.CityDropDown.Position = [782 231 99 22];
% Create CityEditFieldLabel
app.CityEditFieldLabel = uilabel(app.UIFigure);
app.CityEditFieldLabel.HorizontalAlignment = 'right';
app.CityEditFieldLabel.Position = [123 231 79 22];
app.CityEditFieldLabel.Text = 'City Edit Field';
% Create CityEditField
app.CityEditField = uieditfield(app.UIFigure, 'text');
app.CityEditField.Position = [217 231 100 22];
% Create AddCityButton
app.AddCityButton = uibutton(app.UIFigure, 'push');
app.AddCityButton.ButtonPushedFcn = createCallbackFcn(app, @AddCityButtonPushed, true);
app.AddCityButton.Position = [324 231 100 23];
app.AddCityButton.Text = 'Add City';
% Create GetInfoButton
app.GetInfoButton = uibutton(app.UIFigure, 'push');
app.GetInfoButton.ButtonPushedFcn = createCallbackFcn(app, @GetInfoButtonPushed, true);
app.GetInfoButton.Position = [519 108 100 23];
app.GetInfoButton.Text = 'Get Info';
% Show the figure after all components are created
app.UIFigure.Visible = 'on';
end
end
% App creation and deletion
methods (Access = public)
% Construct app
function app = app2
% Create UIFigure and components
createComponents(app)
% Register the app with App Designer
registerApp(app, app.UIFigure)
if nargout == 0
clear app
end
end
% Code that executes before app deletion
function delete(app)
% Delete UIFigure when app is deleted
delete(app.UIFigure)
end
end
end
ERRORS :
Dot indexing is not supported for variables of this type.
Error in fetchFiveDayForecast (line 19)
data(i).dt = datetime(list(i).dt_txt, 'InputFormat', 'yyyy-MM-dd HH:mm:ss', 'Format', 'yyyy-MM-dd HH:mm:ss', 'TimeZone', 'UTC');
weatherData = fetchFiveDayForecast(selectedCity, app.apiKey);
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.
  1 件のコメント
Image Analyst
Image Analyst 2024 年 5 月 26 日
編集済み: Image Analyst 2024 年 5 月 26 日
You already posted this question before and we said it was likely that you had not initialized the "list" variable. Make it easy for people to run your code by attaching the .mlapp files with the paperclip icon.

サインインしてコメントする。

回答 (1 件)

SACHIN KHANDELWAL
SACHIN KHANDELWAL 2024 年 6 月 28 日
I had a brief look into the code that you provided, and I identified where the issue originates. In the function "fetchFiveDayForecast", the response you receive from webread is actually a cell array. To access the data from this cell array, you need to use "{}" braces instead of "()". After making this change to the "list", you should be able to continue with your workflow seamlessly.
You may want to take a look at the following working code.
function weatherData = uv()
apiKey = '8837203c2a21882aa541f6d408e564ab';
city = 'Adana';
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
if isfield(response, 'list')
list = response.list; % list is a cell array
else
error('Invalid response structure');
end
% Initialize data struct
data = struct();
numEntries = length(list);
% Loop through each entry in the list and extract data
for i = 1:numEntries
% to access the data from cell array, use {} instead of ()
%% list(i) will give you again an cell array with struct.
%% list{1} will give you the struct data
data(i).dt = datetime(list{i}.dt_txt, 'InputFormat', 'yyyy-MM-dd HH:mm:ss', 'Format', 'yyyy-MM-dd HH:mm:ss', 'TimeZone', 'UTC');
data(i).temp = list{i}.main.temp;
data(i).humidity = list{i}.main.humidity;
data(i).windSpeed = list{i}.wind.speed;
end
% Store the data in the output structure
weatherData = struct();
weatherData.dt = [data.dt];
weatherData.temp = [data.temp];
weatherData.humidity = [data.humidity];
weatherData.windSpeed = [data.windSpeed];
end
I hope this is helpful.
Thanks!

カテゴリ

Help Center および File ExchangeEnvironment and Settings についてさらに検索

タグ

製品


リリース

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by