uicontrol回调函数输入参数太多

时间:2015-11-13 18:46:56

标签: matlab oop matlab-figure uicontrol matlab-class

我的目标是从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 文本框中获取用户的输入,对输入执行操作,然后将输出显示到另一个文本框。 MATLAB给出了错误:

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

这是我的代码:

{{1}}

1 个答案:

答案 0 :(得分:0)

回调方法实际上有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