Reading data from text box

37 views (last 30 days)
Steven
Steven on 19 Dec 2011
I am simply trying to get user inputted data from a text box and read that data in a push button. Shouldn't it be as simple as a get(handles.DisplayNumber,'String'); function in the push button?
function DisplayNumber_CreateFcn(hObject, eventdata, handles) %text box
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
% --- Executes on button press in Lookup.
function Lookup_Callback(hObject, eventdata, handles) %Push button
N = get(handles.DisplayNumber,'String');
  3 Comments
Jan
Jan on 1 Aug 2013
Edited: Jan on 1 Aug 2013
No, Muhammad, I do not agree. The answer, independent of which answer you actually mean, is neither stupid nor very stupid. Naz's answer solves the problem exhaustively and Steven has accepted it as a solution.
It would be polite if you do not post harsh opinions about the quality of other answers, but offer better solutions, if you think you know one. Thanks.

Sign in to comment.

Accepted Answer

Naz
Naz on 19 Dec 2011
Yes. However, as soon as the button function is executed, your N is deleted. If you want to keep the data in memory, you should include the data in the main struct:
handles.N = get(handles.DisplayNumber,'String');
and then make sure to save it before leaving the fucntion:
guidata(hObject, handles);
And from that point on you should reference your variable as handles.N
The reading of the value from the textbox is correct. I would delete your button and the textbox and create new ones just to make sure I reference the correct objects. Otherwise, go over the objects and check if the tags correspond to the names you reference in the code. Also, every time you make changes in the GUI, you need to save it in addition to your .m file
  4 Comments
Steven
Steven on 19 Dec 2011
Started from scratch and it worked
must have edited the wrong thing thanks for everyone's help!!

Sign in to comment.

More Answers (1)

Eric Keller
Eric Keller on 19 Dec 2011
don't you have to load the "handles" data? I don't use that functionality, but that's my understanding of how it works
  3 Comments
Walter Roberson
Walter Roberson on 19 Dec 2011
I can tell from the code that Steven is using GUIDE; GUIDE callbacks automatically pass the handles structure in.

Sign in to comment.

Categories

Find more on Interactive Control and Callbacks in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!