Matlab:为 GUI 编辑文本设置默认值并在按钮回调中使用它们

Matlab: set default values for GUI edit text and use them in push button callback

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

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

如果用户更改值,我将更新后的值存储在 _Callback 函数内的句柄中,使用 get(hObject, ...) 读取值并使用 guidata(hObject, handles);[=26 更新 guidata =]

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

中提取值

发生以下情况:

我错过了什么?

更新

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 文件一切正常 - 如果我按下 .m 文件上的 run 按钮,就会发生上述奇怪的行为

代码如下:

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);

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