Function GUI问题,无法使用句柄存储变量

Function GUI问题,无法使用句柄存储变量,function,matlab,user-interface,matlab-guide,Function,Matlab,User Interface,Matlab Guide,我正在创建一个GUI,用户在其中输入一个值,当他按下一个按钮时,它运行一个外部函数并显示错误消息。我在GUI编码中成功插入变量时遇到问题。我不知道在哪里插入变量。我试过把手,但不幸的是它不起作用 % --- Executes just before Stallfunction is made visible. function Stallfunction_OpeningFcn(hObject, ~, handles, varargin) % This function has no

我正在创建一个GUI,用户在其中输入一个值,当他按下一个按钮时,它运行一个外部函数并显示错误消息。我在GUI编码中成功插入变量时遇到问题。我不知道在哪里插入变量。我试过把手,但不幸的是它不起作用

  % --- Executes just before Stallfunction is made visible.
  function Stallfunction_OpeningFcn(hObject, ~, handles, varargin)
  % This function has no output args, see OutputFcn.
  % hObject    handle to figure
  % eventdata  reserved - to be defined in a future version of MATLAB
  % handles    structure with handles and user data (see GUIDATA)
  % varargin   command line arguments to Stallfunction (see VARARGIN)
  % Choose default command line output for Stallfunction
   handles.user_entry = user_entry;
  % Update handles structure
  guidata(hObject, handles);
  % UIWAIT makes Stallfunction wait for user response (see UIRESUME)
  % uiwait(handles.figure1);

我在上述代码中插入了变量,即“用户输入”,是否正确?

用户输入未在函数中赋值。如果您通过如下方式为
user\u entry
传递值来启动GUI:

Stallfunction(user_entry)
那么openingFcn中的第一行代码应该是:

if ~isempty(varargin)
    user_entry = varargin{1};
else
    error('please start the GUI with an input value')
end

在此之后,您可以将
user\u entry
分配给handles结构,就像您已经做的那样。

user\u entry
在函数中没有分配值。如果您通过如下方式为
user\u entry
传递值来启动GUI:

Stallfunction(user_entry)
那么openingFcn中的第一行代码应该是:

if ~isempty(varargin)
    user_entry = varargin{1};
else
    error('please start the GUI with an input value')
end
在此之后,您可以将
user\u entry
分配给handles结构,就像您已经做的那样。

尝试以下操作:

function num = get_num()
    fig = figure('Units', 'characters', ...
                 'Position', [70 20 30 5], ...
                 'CloseRequestFcn', @close_Callback);

    edit_num = uicontrol(...
                'Parent', fig, ...
                'Style', 'edit', ...
                'Units', 'characters', ...
                'Position', [1 1 10 3], ...
                'HorizontalAlignment', 'left', ...            
                'String', 'init', ...
                'Callback', @edit_num_Callback);  

    button_finish = uicontrol( ...
        'Parent', fig, ...
        'Tag', 'button_finish', ...
        'Style', 'pushbutton', ...
        'Units', 'characters', ...
        'Position', [15 1 10 3], ...
        'String', 'Finish', ...
        'Callback', @button_finish_Callback);            

    % Nested functions
    function edit_num_Callback(hObject,eventdata)
        disp('this is a callback for edit box');
    end            

    function button_finish_Callback(hObject,eventdata) 
        % Exit
        close(fig);
    end       

    function  close_Callback(hObject,eventdata)
        num_prelim = str2num(get(edit_num,'string'));
        if(isempty(num_prelim))
            errordlg('Must be a number.','Error','modal');
            return;
        end
        num = num_prelim;
        delete(fig);
    end

waitfor(fig);  
end
看看你能不能搞乱这件事,得到你想要的。另外,学习使用嵌套函数以及回调如何在matlab中工作。将此保存为函数文件,然后调用“num=getnum;”

尝试以下操作:

function num = get_num()
    fig = figure('Units', 'characters', ...
                 'Position', [70 20 30 5], ...
                 'CloseRequestFcn', @close_Callback);

    edit_num = uicontrol(...
                'Parent', fig, ...
                'Style', 'edit', ...
                'Units', 'characters', ...
                'Position', [1 1 10 3], ...
                'HorizontalAlignment', 'left', ...            
                'String', 'init', ...
                'Callback', @edit_num_Callback);  

    button_finish = uicontrol( ...
        'Parent', fig, ...
        'Tag', 'button_finish', ...
        'Style', 'pushbutton', ...
        'Units', 'characters', ...
        'Position', [15 1 10 3], ...
        'String', 'Finish', ...
        'Callback', @button_finish_Callback);            

    % Nested functions
    function edit_num_Callback(hObject,eventdata)
        disp('this is a callback for edit box');
    end            

    function button_finish_Callback(hObject,eventdata) 
        % Exit
        close(fig);
    end       

    function  close_Callback(hObject,eventdata)
        num_prelim = str2num(get(edit_num,'string'));
        if(isempty(num_prelim))
            errordlg('Must be a number.','Error','modal');
            return;
        end
        num = num_prelim;
        delete(fig);
    end

waitfor(fig);  
end

看看你能不能搞乱这件事,得到你想要的。另外,学习使用嵌套函数以及回调如何在matlab中工作。将其保存为函数文件,然后调用“num=getnum;”

GUI不会通过传递值启动。我有GUI界面,然后在编辑框中输入一个值,按下按钮运行另一个函数。是否与您所指的内容相同?@user1860036:在这种情况下,您必须填写editbox的回调,以便将该框的内容写入
句柄。user\u entry
。GUI不会通过传递值启动。我有GUI界面,然后在编辑框中输入一个值,按下按钮运行另一个函数。这与您所指的是同一件事吗?@user1860036:在这种情况下,您必须填写editbox的回调,以便将该框的内容写入
句柄。user\u entry