0

我正在尝试使用默认值初始化 GUI(使用 GUIDE 构建),然后,如果用户不更改默认值,请在按钮回调触发的函数中使用这些值。

为此,在 中_CreateFcn,我首先将默认值存储在 中handles,然后使用 设置 GUI 的默认值set(hObject, ...),最后使用 更新 guidataguidata(hObject, handles);

如果用户更改了值,我将更新的值存储在_Callback函数内部的句柄中,读取值get(hObject, ...)并更新 guidataguidata(hObject, handles);

当按下按钮时,在按钮_Callback函数内部,我从handles.

发生的情况如下:

  • 如果用户改变 GUI 上的值而只是按下按钮,我从句柄中存储的变量中读出的不是变量的值,而是变量的实际句柄(例如27.0098876953125:)
  • 另一方面,如果用户在按下按钮之前确实更改了值,那么一切正常,我得到了实际的变量值。

我错过了什么?

更新

oro777评论之后,我添加了其余代码以便更好地分析。我也尝试过使用更新的(R2015b)版本的 MATLAB,结果是一样的,不同的是现在disp按钮回调函数内部显示了整个句柄结构,而不仅仅是 id:

UIControl (ampmin) with properties:

          Style: 'edit'
         String: '1'
BackgroundColor: [1 1 1]
       Callback: @(hObject,eventdata)GUI('ampmin_Callback',hObject,eventdata,guidata(hObject))
          Value: 0
       Position: [15.6000 14.6154 10.2000 1.6923]
          Units: 'characters'

Use get to show all properties

我还注意到以下情况: - 如果我启动.fig文件一切正常 - 如果我按下文件run上的按钮.m,则会发生上述奇怪行为

这是代码:

function varargout = GUI2(varargin)
% GUI2 MATLAB code for GUI2.fig
%      GUI2, by itself, creates a new GUI2 or raises the existing
%      singleton*.
%
%      H = GUI2 returns the handle to a new GUI2 or the handle to
%      the existing singleton*.
%
%      GUI2('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in GUI2.M with the given input arguments.
%
%      GUI2('Property','Value',...) creates a new GUI2 or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI2 before GUI2_OpeningFcn gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to GUI2_OpeningFcn via varargin.
%
%      *See GUI2 Options on GUIDE Tools menu.  Choose "GUI2 allows only one
%      instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES

% Edit the above text to modify the response to help GUI2

% Last Modified by GUIDE v2.5 14-Aug-2015 10:39:46

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @GUI2_OpeningFcn, ...
                   'gui_OutputFcn',  @GUI2_OutputFcn, ...
                   'gui_LayoutFcn',  [] , ...
                   'gui_Callback',   []);
if nargin && ischar(varargin{1})
    gui_State.gui_Callback = str2func(varargin{1});
end

if nargout
    [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
    gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT


% --- Executes just before GUI2 is made visible.
function GUI2_OpeningFcn(hObject, eventdata, 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 GUI2 (see VARARGIN)

% Choose default command line output for GUI2
handles.output = hObject;

% Update handles structure
guidata(hObject, handles);

% UIWAIT makes GUI2 wait for user response (see UIRESUME)
% uiwait(handles.figure1);


% --- Outputs from this function are returned to the command line.
function varargout = GUI2_OutputFcn(hObject, eventdata, handles) 
% varargout  cell array for returning output args (see VARARGOUT);
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Get default command line output from handles structure
varargout{1} = handles.output;


% --- Executes on button press in startAnalysis.
function startAnalysis_Callback(hObject, eventdata, handles)
% hObject    handle to startAnalysis (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

disp(handles.ampmin)





function ampmin_Callback(hObject, eventdata, handles)
% hObject    handle to ampmin (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of ampmin as text
%        str2double(get(hObject,'String')) returns contents of ampmin as a double
handles.ampmin = str2double(get(hObject,'String'));

% Update handles structure
guidata(hObject, handles);

% --- Executes during object creation, after setting all properties.
function ampmin_CreateFcn(hObject, eventdata, handles)
% hObject    handle to ampmin (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end

handles.ampmin = 1.0;
disp(handles.ampmin)
set(hObject, 'String', num2str(handles.ampmin))

% Update handles structure
guidata(hObject, handles);
4

1 回答 1

0

谢谢@Hoki,这就是问题所在。由于我没有看到任何直接引用,handles.ampmin因此我认为它不会用于存储句柄,但是查看实际.fig导出的代码,很明显确实uicontrol是存储了句柄。

于 2015-08-14T15:49:44.023 回答