NaN Error in "App Designer"
10 ビュー (過去 30 日間)
古いコメントを表示
Hey guys,
I try to write a simple app that shows me websites in my local browser, I evaluate them with one click and then the next page is loaded. In each step my selection should be written "ratedURLs", which is stored in data_rated.mat. Now the problem is that when I load data_rated.mat into my Matlab workspace, all my values are displayed as "NaN". It is correct that my selection is a string and not a number, but it should not really matter?
classdef manuel_rating_app < matlab.apps.AppBase
% Properties that correspond to app components
properties (Access = public)
UIFigure matlab.ui.Figure
HilightButton matlab.ui.control.Button
VerypositivButton matlab.ui.control.Button
PositivButton matlab.ui.control.Button
NeutralButton matlab.ui.control.Button
PoorButton matlab.ui.control.Button
BadButton matlab.ui.control.Button
VeryBadButton matlab.ui.control.Button
StatusEditFieldLabel matlab.ui.control.Label
StatusEditField matlab.ui.control.EditField
end
properties (Access = public)
URL % Description
NewsData
RatedURLs
end
methods (Access = public)
function writeData(app,status)
app.RatedURLs(app.URL,1) = status;
ratedURLs = app.RatedURLs;
save('data_rated.mat');
app.URL = app.URL + 1;
app.StatusEditField.Value = "Aktuelle Webseite: " + num2str(app.URL);
web(app.NewsData(app.URL,3),'-browser');
end
end
methods (Access = private)
% Code that executes after component creation
function startupFcn(app)
try
load('data.mat','newsData')
app.URL = 1;
app.NewsData = newsData;
app.StatusEditField.Value = "Loading succeeded";
web(newsData(1,3),'-browser');
catch ERROR
error = ERROR.message;
app.StatusEditField.Value = error;
drawnow;
end
end
% Button pushed function: VerypositivButton
function VerypositivButtonPushed(app, event)
writeData(app,"very Positiv")
end
% Button pushed function: HilightButton
function HilightButtonPushed(app, event)
writeData(app,"Highlight")
end
% Button pushed function: PositivButton
function PositivButtonPushed(app, event)
writeData(app,"Positiv")
end
% Button pushed function: NeutralButton
function NeutralButtonPushed(app, event)
writeData(app,"Neutral")
end
% Button pushed function: PoorButton
function PoorButtonPushed(app, event)
writeData(app,"Poor")
end
% Button pushed function: BadButton
function BadButtonPushed(app, event)
writeData(app,"Bad")
end
% Button pushed function: VeryBadButton
function VeryBadButtonPushed(app, event)
writeData(app,"very Bad")
end
end
% App initialization and construction
methods (Access = private)
% Create UIFigure and components
function createComponents(app)
% Create UIFigure
app.UIFigure = uifigure;
app.UIFigure.Position = [100 100 739 962];
app.UIFigure.Name = 'UI Figure';
% Create HilightButton
app.HilightButton = uibutton(app.UIFigure, 'push');
app.HilightButton.ButtonPushedFcn = createCallbackFcn(app, @HilightButtonPushed, true);
app.HilightButton.Position = [81 835 197 103];
app.HilightButton.Text = 'Hilight';
% Create VerypositivButton
app.VerypositivButton = uibutton(app.UIFigure, 'push');
app.VerypositivButton.ButtonPushedFcn = createCallbackFcn(app, @VerypositivButtonPushed, true);
app.VerypositivButton.Position = [81 701 197 103];
app.VerypositivButton.Text = 'Very positiv';
% Create PositivButton
app.PositivButton = uibutton(app.UIFigure, 'push');
app.PositivButton.ButtonPushedFcn = createCallbackFcn(app, @PositivButtonPushed, true);
app.PositivButton.Position = [81 571 197 103];
app.PositivButton.Text = 'Positiv';
% Create NeutralButton
app.NeutralButton = uibutton(app.UIFigure, 'push');
app.NeutralButton.ButtonPushedFcn = createCallbackFcn(app, @NeutralButtonPushed, true);
app.NeutralButton.Position = [81 441 197 103];
app.NeutralButton.Text = 'Neutral';
% Create PoorButton
app.PoorButton = uibutton(app.UIFigure, 'push');
app.PoorButton.ButtonPushedFcn = createCallbackFcn(app, @PoorButtonPushed, true);
app.PoorButton.Position = [81 311 197 103];
app.PoorButton.Text = 'Poor';
% Create BadButton
app.BadButton = uibutton(app.UIFigure, 'push');
app.BadButton.ButtonPushedFcn = createCallbackFcn(app, @BadButtonPushed, true);
app.BadButton.Position = [81 180 197 103];
app.BadButton.Text = 'Bad';
% Create VeryBadButton
app.VeryBadButton = uibutton(app.UIFigure, 'push');
app.VeryBadButton.ButtonPushedFcn = createCallbackFcn(app, @VeryBadButtonPushed, true);
app.VeryBadButton.Position = [82 50 197 103];
app.VeryBadButton.Text = 'Very Bad';
% Create StatusEditFieldLabel
app.StatusEditFieldLabel = uilabel(app.UIFigure);
app.StatusEditFieldLabel.HorizontalAlignment = 'right';
app.StatusEditFieldLabel.Position = [378 471 40 22];
app.StatusEditFieldLabel.Text = 'Status';
% Create StatusEditField
app.StatusEditField = uieditfield(app.UIFigure, 'text');
app.StatusEditField.Editable = 'off';
app.StatusEditField.Position = [433 471 274 22];
end
end
methods (Access = public)
% Construct app
function app = manuel_rating_app
% Create and configure components
createComponents(app)
% Register the app with App Designer
registerApp(app, app.UIFigure)
% Execute the startup function
runStartupFcn(app, @startupFcn)
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
0 件のコメント
回答 (1 件)
Kanishk
2024 年 12 月 23 日 6:20
When debugging the code with a breakpoint on line 26, it appears that "app.RatedURLs" is being updated with "NaN" values at line 25:
25: app.RatedURLs(app.URL,1) = status
26: ratedURLs = app.RatedURLs
This issue arises because the "RatedURLs" property has not been initialized in the properties section. As a result, MATLAB initializes this property to an empty double ([]). Consequently, when attempting to assign string values to this property within the function, it results in "NaN" values.
To resolve this issue, you can restrict the "RatedURLs" property to accept string values by declaring it as such in the properties section. Here is an example of how you can achieve this:
properties (Access = public)
URL % Description
NewsData
RatedURLs string
end
You can read more about initializing property values by referring to the following link.
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Develop uifigure-Based Apps についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!