【问题标题】:Matlab: set default values for GUI edit text and use them in push button callbackMatlab:为 GUI 编辑文本设置默认值并在按钮回调中使用它们
【发布时间】:2015-08-14 01:18:02
【问题描述】:

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

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

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

按下按钮时,在按钮_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 文件一切正常 - 如果我按下.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);

【问题讨论】:

  • 我试过你的代码,它对我来说很好用。我猜你的代码的其余部分搞砸了。
  • 您遇到了问题,因为您使用自定义值覆盖句柄。你不应该在handle.ampmin 上面写任何东西,这是由GUIDE 设置的,以保留uicontrol 的句柄。如果要将数据存储在 handles 结构中,请为其定义一个新的自定义字段:handles.ampminValue = str2double(get(hObject,'String'));

标签: matlab parameter-passing setdefault


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-07-24
    • 1970-01-01
    • 1970-01-01
    • 2016-10-29
    • 1970-01-01
    • 1970-01-01
    • 2020-09-15
    • 2014-06-07
    相关资源
    最近更新 更多