如何从GUI中的编辑框中检索数据

时间:2013-03-11 21:00:54

标签: function matlab user-interface user-defined-functions

我的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 个答案:

答案 0 :(得分:0)

看看这个: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将返回“[]。”