如何从 GUI 中的编辑框中检索数据
Posted
技术标签:
【中文标题】如何从 GUI 中的编辑框中检索数据【英文标题】:How to retrieve data from an editbox in a GUI 【发布时间】:2013-03-11 21:00:53 【问题描述】:我的 GUI 中有一个编辑框。用户在编辑框中输入一个数字,然后按下按钮。当按下按钮时,调用外部函数。对于外部函数,我需要在编辑框中输入的数字。如何使用“句柄”检索编辑框中的数据输入?
这是我打开函数的代码
% --- Executes just before NEWSTALLGUI is made visible.
function NEWSTALLGUI_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 NEWSTALLGUI (see VARARGIN)
% Choose default command line output for NEWSTALLGUI
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
set(handles.EnterSpeed,'string','0');
% UIWAIT makes NEWSTALLGUI wait for user response (see UIRESUME)
% uiwait(handles.figure1);
这是我的编辑框的代码
function EnterSpeed_Callback(hObject, eventdata, handles)
% hObject handle to EnterSpeed (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 EnterSpeed as text
% str2double(get(hObject,'String')) returns contents of EnterSpeed as a double
user_speed=str2double(get(handles.EnterSpeed,'string'));
这是我的按钮的代码
% --- Executes on button press in Calculate.
function Calculate_Callback(hObject, eventdata, handles)
% hObject handle to Calculate (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
Newfunction(user_speed);
【问题讨论】:
【参考方案1】:看看这个:Problems with GUI, unable to use handles to store variables
基本上使用: editnum = get(editbox_handle,'string');
那么您可能希望将其转换为数字。
编辑:
function NEWSTALLGUI_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 NEWSTALLGUI (see VARARGIN)
% Choose default command line output for NEWSTALLGUI
handles.output = hObject;
handles.user_speed = 0;
% Update handles structure
guidata(hObject, handles);
set(handles.EnterSpeed,'string','0');
% UIWAIT makes NEWSTALLGUI wait for user response (see UIRESUME)
% uiwait(handles.figure1);
-
function EnterSpeed_Callback(hObject, eventdata, handles)
% hObject handle to EnterSpeed (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 EnterSpeed as text
% str2double(get(hObject,'String')) returns contents of EnterSpeed as a double
handles.user_speed=str2double(get(handles.EnterSpeed,'string'));
-
function Calculate_Callback(hObject, eventdata, handles)
% hObject handle to Calculate (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
Newfunction(handles.user_speed);
-
我认为您也可以使用:
Newfunction(str2double(get(handles.EnterSpeed,'string')));
但不管你的船是什么。此外,在调用 Newfunction 之前,您还应该检查输入是否也是数字...在除数字之外的其他内容上使用 str2double 将返回“[]”。
【讨论】:
我已经使用了这个 user_speed=str2double(get(handles.EnterSpeed,'string') 但我仍然收到错误消息。我在打开函数之前将其定义为 0跨度> 在开启函数时使用setappdata(figurehandle,'user_speed',user_speed)。在按钮回调期间,使用 getappdata(figurehandle,'user_speed')。此外,在编辑框回调期间使用 setappdata 来更新 user_speed。如果您发布一些代码会更有帮助。 您需要一种在回调之间传递信息的方法。对于 EnterSpeed_Callback 使用:handles.user_speed = str2double(...)。然后对于 Calculate_Callback 使用:Newfunction(handles.user_speed)。还要确保在打开函数中初始化handles.user_speed = 0。让我知道这个是否奏效。您正在使用 GUIDE,所以我正在按照我记得的内容来做这件事。检查我编辑的答案。以上是关于如何从 GUI 中的编辑框中检索数据的主要内容,如果未能解决你的问题,请参考以下文章
mfc如何将一个编辑框中的数字追加到另一个编辑框,不是覆盖,而是接在原来的后面显示?