1

我正在尝试收集NumericEditField用户输入到控件中的几个值并将它们存储起来,以便稍后在计算中使用它们,其结果会被返回。

我似乎能够存储变量,但以后无法访问它们。每个变量都被认为是私有的,因为这是让我添加回调的地方。我曾尝试在开头附近的公共部分添加所有变量,但这不起作用。

这是代码:

classdef Finalscalc < matlab.apps.AppBase

    % Properties that correspond to app components
    properties (Access = public)
        UIFigure                       matlab.ui.Figure
        PleaseenteryourcurrentgradeEditFieldLabel  matlab.ui.control.Label
        PleaseenteryourcurrentgradeEditField  matlab.ui.control.NumericEditField
        PleaseenterthegradeyouwantEditFieldLabel  matlab.ui.control.Label
        PleaseenterthegradeyouwantEditField  matlab.ui.control.NumericEditField
        PleaseentertheweightofthefinalexamEditFieldLabel  matlab.ui.control.Label
        PleaseentertheweightofthefinalexamEditField  matlab.ui.control.NumericEditField
        Label                          matlab.ui.control.Label
        Label_2                        matlab.ui.control.Label
        Label_3                        matlab.ui.control.Label
        YouneedatleastaEditFieldLabel  matlab.ui.control.Label
        YouneedatleastaEditField       matlab.ui.control.NumericEditField
        Label_4                        matlab.ui.control.Label
        togetaEditFieldLabel           matlab.ui.control.Label
        togetaEditField                matlab.ui.control.NumericEditField
        Label_5                        matlab.ui.control.Label
        CalcualteButton                matlab.ui.control.Button
        GoodLuckLabel                  matlab.ui.control.Label
    end



    methods (Access = private)

        % Code that executes after component creation
        function startupFcn(app)

        end

        % Value changed function: PleaseenteryourcurrentgradeEditField
        function PleaseenteryourcurrentgradeEditFieldValueChanged(app, event)
            app.current = app.PleaseenteryourcurrentgradeEditField.Value;
            current = app.current;
            app.current = current;
        end

        % Value changed function: PleaseenterthegradeyouwantEditField
        function PleaseenterthegradeyouwantEditFieldValueChanged(app, event)
            app.want = app.PleaseenterthegradeyouwantEditField.Value;
            want = app.want;
            app.want = want;
        end

        % Value changed function: 
        % PleaseentertheweightofthefinalexamEditField
        function PleaseentertheweightofthefinalexamEditFieldValueChanged(app, event)
            app.weight = app.PleaseentertheweightofthefinalexamEditField.Value;
            weight = app.weight;
            app.weight = weight;
        end

        % Button pushed function: CalcualteButton
        function CalcualteButtonPushed(app, event)
            grade = ((100 * app.want)-(100 - app.weight) * app.current) / app.weight;
            grade = app.grade;
            app.grade = grade;
        end

        % Value changed function: YouneedatleastaEditField
        function YouneedatleastaEditFieldValueChanged(app, event)
            grade = app.YouneedatleastaEditField.Value;
            fprintf('%,2f', grade); 
        end

        % Value changed function: togetaEditField
        function togetaEditFieldValueChanged(app, event)
            app.PleaseenterthegradeyouwantEditField.Value = app.togetaEditField.Value;

        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 640 480];
            app.UIFigure.Name = 'UI Figure';

            % Create PleaseenteryourcurrentgradeEditFieldLabel
            app.PleaseenteryourcurrentgradeEditFieldLabel = uilabel(app.UIFigure);
            app.PleaseenteryourcurrentgradeEditFieldLabel.HorizontalAlignment = 'right';
            app.PleaseenteryourcurrentgradeEditFieldLabel.Position = [117 337 174 22];
            app.PleaseenteryourcurrentgradeEditFieldLabel.Text = 'Please enter your current grade';

            % Create PleaseenteryourcurrentgradeEditField
            app.PleaseenteryourcurrentgradeEditField = uieditfield(app.UIFigure, 'numeric');
            app.PleaseenteryourcurrentgradeEditField.ValueChangedFcn = createCallbackFcn(app, @PleaseenteryourcurrentgradeEditFieldValueChanged, true);
            app.PleaseenteryourcurrentgradeEditField.Position = [306 337 100 22];

            % Create PleaseenterthegradeyouwantEditFieldLabel
            app.PleaseenterthegradeyouwantEditFieldLabel = uilabel(app.UIFigure);
            app.PleaseenterthegradeyouwantEditFieldLabel.HorizontalAlignment = 'right';
            app.PleaseenterthegradeyouwantEditFieldLabel.Position = [116 289 178 22];
            app.PleaseenterthegradeyouwantEditFieldLabel.Text = 'Please enter the grade you want';

            % Create PleaseenterthegradeyouwantEditField
            app.PleaseenterthegradeyouwantEditField = uieditfield(app.UIFigure, 'numeric');
            app.PleaseenterthegradeyouwantEditField.ValueChangedFcn = createCallbackFcn(app, @PleaseenterthegradeyouwantEditFieldValueChanged, true);
            app.PleaseenterthegradeyouwantEditField.Position = [309 289 100 22];

            % Create PleaseentertheweightofthefinalexamEditFieldLabel
            app.PleaseentertheweightofthefinalexamEditFieldLabel = uilabel(app.UIFigure);
            app.PleaseentertheweightofthefinalexamEditFieldLabel.HorizontalAlignment = 'right';
            app.PleaseentertheweightofthefinalexamEditFieldLabel.Position = [70 248 224 22];
            app.PleaseentertheweightofthefinalexamEditFieldLabel.Text = 'Please enter the weight of the final exam';

            % Create PleaseentertheweightofthefinalexamEditField
            app.PleaseentertheweightofthefinalexamEditField = uieditfield(app.UIFigure, 'numeric');
            app.PleaseentertheweightofthefinalexamEditField.ValueChangedFcn = createCallbackFcn(app, @PleaseentertheweightofthefinalexamEditFieldValueChanged, true);
            app.PleaseentertheweightofthefinalexamEditField.Position = [309 248 100 22];

            % Create Label
            app.Label = uilabel(app.UIFigure);
            app.Label.Position = [417 337 25 22];
            app.Label.Text = '%';

            % Create Label_2
            app.Label_2 = uilabel(app.UIFigure);
            app.Label_2.Position = [417 289 25 22];
            app.Label_2.Text = '%';

            % Create Label_3
            app.Label_3 = uilabel(app.UIFigure);
            app.Label_3.Position = [417 248 25 22];
            app.Label_3.Text = '%';

            % Create YouneedatleastaEditFieldLabel
            app.YouneedatleastaEditFieldLabel = uilabel(app.UIFigure);
            app.YouneedatleastaEditFieldLabel.HorizontalAlignment = 'right';
            app.YouneedatleastaEditFieldLabel.Position = [177 105 107 22];
            app.YouneedatleastaEditFieldLabel.Text = 'You need at least a';

            % Create YouneedatleastaEditField
            app.YouneedatleastaEditField = uieditfield(app.UIFigure, 'numeric');
            app.YouneedatleastaEditField.ValueChangedFcn = createCallbackFcn(app, @YouneedatleastaEditFieldValueChanged, true);
            app.YouneedatleastaEditField.Position = [291 105 19 22];

            % Create Label_4
            app.Label_4 = uilabel(app.UIFigure);
            app.Label_4.Position = [323 105 25 22];
            app.Label_4.Text = '%';

            % Create togetaEditFieldLabel
            app.togetaEditFieldLabel = uilabel(app.UIFigure);
            app.togetaEditFieldLabel.HorizontalAlignment = 'right';
            app.togetaEditFieldLabel.Position = [337 105 46 22];
            app.togetaEditFieldLabel.Text = 'to get a';

            % Create togetaEditField
            app.togetaEditField = uieditfield(app.UIFigure, 'numeric');
            app.togetaEditField.ValueChangedFcn = createCallbackFcn(app, @togetaEditFieldValueChanged, true);
            app.togetaEditField.Position = [390 105 16 22];

            % Create Label_5
            app.Label_5 = uilabel(app.UIFigure);
            app.Label_5.Position = [417 105 25 22];
            app.Label_5.Text = '%';

            % Create CalcualteButton
            app.CalcualteButton = uibutton(app.UIFigure, 'push');
            app.CalcualteButton.ButtonPushedFcn = createCallbackFcn(app, @CalcualteButtonPushed, true);
            app.CalcualteButton.Position = [271 168 100 22];
            app.CalcualteButton.Text = 'Calcualte';

            % Create GoodLuckLabel
            app.GoodLuckLabel = uilabel(app.UIFigure);
            app.GoodLuckLabel.Position = [287 30 70 22];
            app.GoodLuckLabel.Text = 'Good Luck !';
        end
    end

    methods (Access = public)

        % Construct app
        function app = Finalscalc

            % 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
4

1 回答 1

0

无需使用...ValueChanged函数,您只需访问Value要从中获取值的各种控件的字段。private methods用这个替换你当前的块:

methods (Access = private)

    % Button pushed function: CalcualteButton
    function CalcualteButtonPushed(app, ~)
      % Get values from edit fields:
        want = app.PleaseenterthegradeyouwantEditField.Value;
        weight = app.PleaseentertheweightofthefinalexamEditField.Value;
        current = app.PleaseenteryourcurrentgradeEditField.Value;          
      % Perform computation:
        needed = ( (100 * want) - (100 - weight) * current) / weight;
      % Write values to new edit fields:  
        app.YouneedatleastaEditField.Value = needed;
        app.togetaEditField.Value = want;
    end

end

在我看来,TMW 极大地简化了从新 UIFigure 系统中的文本字段访问值的过程——从这里开始,只需了解它是如何工作的。

于 2018-11-12T08:19:08.037 回答