uicontrol 回调函数输入参数过多

uicontrol callback function too many input arguments

我的目标是从 uicontrol 文本框中获取用户的输入,对输入进行操作,然后将输出显示到另一个文本框。 MATLAB 给我错误:

Error using
UnitConverter/lbs2kg
Too many input arguments.

Error in
UnitConverter>@(varargin)app.lbs2kg(varargin{:})
(line 22)
                'Callback',@app.lbs2kg,'String',app.inputMass); 
Error while evaluating UIControl Callback

这是我的代码:

classdef UnitConverter < handle


    properties
        Figure                  % Graphics handles
        DispInputMass
        DispOutputMass

        inputMass               %Variables/Class Properties
        outputMass 
    end


    methods

        function app = UnitConverter
            % This is the "constructor" for the class
            % It runs when an object of this class is created
            app.Figure = figure('Name','Unit Converter') ;

            app.DispInputMass = uicontrol('Style','edit',...
                'Callback',@app.lbs2kg,'String',app.inputMass);

            app.DispOutputMass = uicontrol(app.Figure,'Style','edit','Position'...
                 ,[168 100 47 26],'String','kg');
        end



        function lbs2kg(app,evt)
            app.inputMass = get(app.DispInputMass,'string');
            app.outputMass = app.inputMass*.453;
            set(app.DispOutputMass,'string',app.outputMass);
        end


    end
end

回调方法实际上有 3 个输入 - MATLAB 抛出此错误是因为它试图将三个输入发送到您的回调,该回调只接受 2 个。这 3 个输入是(按顺序):主对象 ( app), 发送事件的对象 (uicontrol) 和事件 (matlab.ui.eventdata.ActionData).

您可以将代码更改为以下代码以使其正常工作:

function lbs2kg(app, obj, evt)
    app.inputMass = get(app.DispInputMass,'string');
    app.outputMass = app.inputMass*.453;
    set(app.DispOutputMass,'string',app.outputMass);
end

此外,您可以将函数的第一行更改为以下内容:

function lbs2kg(varargin)

在回调的第一行断点代码并调查 varargin 的内容。有关 varargin 的更多帮助,请参阅此处 (http://www.mathworks.com/help/matlab/ref/varargin.html)