Невозможно получить доступ к значениям, которые пользователь вводит в текстовые поля - PullRequest
0 голосов
/ 11 ноября 2018

Я пытаюсь собрать несколько значений, введенных пользователем в элементы управления 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

1 Ответ

0 голосов
/ 12 ноября 2018

Вместо использования функций ...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 - отсюда просто вопрос понимания того, как она работает.

...