0

我的 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);
4

1 回答 1

0

看这个:GUI问题,无法使用句柄存储变量

基本上使用: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 将返回“[]”。

于 2013-03-11T21:39:29.387 回答