在 MATLAB 中的一个框中从用户那里获取多个输入

Taking multiple inputs from user in one box in MATLAB

我想用 waitfor 运算符获取多个值,但我的代码不起作用。我的错误在哪里?

 methods (Access = private)
        % Callback function
        function ButtonPushed(app, event)
                %Reading dataset and getting the inputs from user by using input operation

                matrix = xlsread("Transfusion.xlsx");
                attributesNum = size(matrix,2) - 1 ;
                X = zeros(attributesNum,1);
                for i=1:attributesNum
                    waitfor(app.firstVal, 'Value');
                        value = app.firstVal.Value;
                        X(i,1) = value;
                    %Update text of ValuesLabel (for demostrating the concept).
                        text = ['Values: ', sprintf('%.1f, ', X(1:i))];
                        app.ValuesLabel.Text = text(1:end-2);
                end

                %Display X in Command window for testing
                disp(X)

                ...
        end

我认为它应该有效...

  • 确保使用 MATLAB App Designer(您可以通过在命令 window 中输入 appdesigner 来启动它)。
  • 确保在 MATLAB App Designer 中将编辑框命名为 firstVal

  • 将值设置为 Inf 是一个临时解决方案,但这很重要,因为 waitfor 等待值被更改。
    输入新值后将值设置为 Inf 会强制用户更改值。
  • 我用 matrix = [0, 0, 0, 0, 0, 0]; 替换了 matrix = xlsread("Transfusion.xlsx");
    备注:不要指望我们猜"Transfusion.xlsx".
    的内容是什么 该文件与 post 无关,因为您只使用了大小。
  • 备注:不要害羞,post整个 App Designer 代码作为参考(当使用像 App Designer 这样的工具时,该工具可能会在自动生成的代码中隐藏一些重要信息,这可能对于识别问题很重要)。
  • 备注:既然是跟进post,你应该给你的一个参考。

这是 function ButtonPushed(app, event) 的修改版本:

% Button pushed function: Button
function ButtonPushed(app, event)
    app.Button.Enable = 'Off'; %Disable button while taking input (just nicer).

    %I replaced the xlsread("Transfusion.xlsx") with some arbitrary values, because only the size is relevant
    matrix = [0, 0, 0, 0, 0, 0]; %xlsread("Transfusion.xlsx"); 
    attributesNum = size(matrix,2) - 1;
    X = zeros(attributesNum, 1);

    %Initialize text label with message
    app.ValuesLabel.Text = ['Enter ', num2str(attributesNum), ' values in edit box (set value and press enter)'];

    for i = 1:attributesNum
        waitfor(app.firstVal, 'Value');
        value = app.firstVal.Value;
        X(i, 1) = value;

        %Set to Inf every iteration, because waitfor waits for a change in value (and you may need to enter same value twice).
        app.firstVal.Value = Inf;

        %Update text of ValuesLabel (for demonstrating the concept).
        text = ['Values: ', sprintf('%.1f, ', X(1:i))];
        app.ValuesLabel.Text = text(1:end-2);
    end

    %Display X in Command window for testing
    disp(X)

    app.Button.Enable = 'On'; %Enable button at the end.
end

这里是完整的代码(包括生成的代码)。
您可以将其复制并粘贴到 App1.m 文件中,看看它是如何工作的。

classdef app1 < matlab.apps.AppBase

    % Properties that correspond to app components
    properties (Access = public)
        UIFigure        matlab.ui.Figure
        EditFieldLabel  matlab.ui.control.Label
        firstVal        matlab.ui.control.NumericEditField
        ValuesLabel     matlab.ui.control.Label
        Button          matlab.ui.control.Button
    end



    % Callbacks that handle component events
    methods (Access = private)

        % Button pushed function: Button
        function ButtonPushed(app, event)
            app.Button.Enable = 'Off'; %Disable button while taking input (just nicer).

            %I replaced the xlsread("Transfusion.xlsx") with some arbitrary values, because only the size is relevant
            matrix = [0, 0, 0, 0, 0, 0]; %xlsread("Transfusion.xlsx"); 
            attributesNum = size(matrix,2) - 1;
            X = zeros(attributesNum, 1);

            %Initialize text label with message
            app.ValuesLabel.Text = ['Enter ', num2str(attributesNum), ' values in edit box (set value and press enter)'];

            for i = 1:attributesNum
                waitfor(app.firstVal, 'Value');
                value = app.firstVal.Value;
                X(i, 1) = value;

                %Set to Inf every iteration, because waitfor waits for a change in value (and you may need to enter same value twice).
                app.firstVal.Value = Inf;

                %Update text of ValuesLabel (for demonstrating the concept).
                text = ['Values: ', sprintf('%.1f, ', X(1:i))];
                app.ValuesLabel.Text = text(1:end-2);
            end

            %Display X in Command window for testing
            disp(X)

            app.Button.Enable = 'On'; %Enable button at the end.
        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 369 256];
            app.UIFigure.Name = 'UI Figure';

            % Create EditFieldLabel
            app.EditFieldLabel = uilabel(app.UIFigure);
            app.EditFieldLabel.HorizontalAlignment = 'right';
            app.EditFieldLabel.Position = [45 123 56 22];
            app.EditFieldLabel.Text = 'Edit Field';

            % Create firstVal
            app.firstVal = uieditfield(app.UIFigure, 'numeric');
            app.firstVal.Position = [116 123 100 22];
            app.firstVal.Value = Inf;

            % Create ValuesLabel
            app.ValuesLabel = uilabel(app.UIFigure);
            app.ValuesLabel.Position = [49 51 297 22];
            app.ValuesLabel.Text = 'Values: ';

            % Create Button
            app.Button = uibutton(app.UIFigure, 'push');
            app.Button.ButtonPushedFcn = createCallbackFcn(app, @ButtonPushed, true);
            app.Button.Tooltip = {'Press to start taking input'};
            app.Button.Position = [46 204 120 32];

            % 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 = app1

            % 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