How to retrieve data entered in an edit box

I have an edit box in my GUI. User inputs a number into the editbox and then pushes a pushbutton. when the pushbutton is pressed an external function is called. for the external function i need the number entered in the editbox. How can i used 'handles' to retrieve the data input in the edit box?

 Accepted Answer

s=str2double(get(handles.editboxname,'string'))
% editboxname is the tag of your edit box.

9 Comments

would I have to define 's' somewhere else, other than when I am using it in my callback function. Do i need to define s=0 before my opening function for example
If you want to set it to 0 in your opening function
set(handles.editboxname,'string','0')
I set it as 0 just before my opening function. my editbox is called EnterSpeed. I have made my GUI three times I keep debugging but i am still coming up with errors for e.g:-
??? Undefined variable "handles" or class "handles.EnterSpeed".
Error in ==> NEWSTALLGUI at 46
set(handles.EnterSpeed,'string','0');
>>
How did you create the name EnterSpeed ?
% --- 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);
I I have set EnterSpeed as 0 in my opening function. When I created the editbox I changed it into EnterSpeed. Do you think it will be easier to work with name such as 'editbox'
No this have nothing to do with the name, I am not sure that the name is EnterSpeed. When you click on view callback of your editbox, which name are you seeing?
I am not sure what you mean exactly tbh.
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'));
This is my callback code for my editbox
The code
user_speed=str2double(get(handles.EnterSpeed,'string'));
You should put it in the pushbutton callback, NOT the edit field callback.
thankyou that has done the trick

Sign in to comment.

More Answers (0)

Categories

Find more on Graphics Objects in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!