MATLAB Answers


Reading data from text box

Asked by 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'))
% --- Executes on button press in Lookup.
function Lookup_Callback(hObject, eventdata, handles) %Push button 
N = get(handles.DisplayNumber,'String');


on 19 Dec 2011


This is a very stupid answer to a question to reading an editbox. no where it is mentioned how editbox data is to be retrieved. He is talking about background color and everything and not the main thing that is required.

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.

Log in to comment.


2 Answers

Answer by Naz
on 19 Dec 2011
 Accepted Answer

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


Show 1 older comment

What do you want done with the string once you have fetched it? Callbacks are not able to pass values back to their caller (because they do not have a caller.) Therefore if you want to use the value of the string outside of the callback, you must export it somehow. Storing it in the handles structure the way Naz shows is one common way of doing that. It doesn't matter whether you name it N or something else as long as the name is a valid field name and does not overwrite anything.

I think we would need to see more code to figure out the cause of the "Invalid handle object" error.

The purpose of the entire program is to take a picture from my computer modify it bases on some information contained in displaylist variable and display it.
Okay the following is the entire Guide I am working with

function DisplayNumber_Callback(hObject, eventdata, handles)

function DisplayNumber_CreateFcn(hObject, eventdata, handles)

if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))

% --- Executes on button press in Lookup.
function Lookup_Callback(hObject, eventdata, handles)
N = get(handles.DisplayNumber,'String');
[displaylist,type] = lookup(N);

the lookup function is written by me and is as follows

function [displaylist,type] = lookup(N)
N = num2str(N);
String = ['Defect Data\displaylist',N];

I plan on taking the output of the lookup function and running an Imshow function to display an image all under the lookup push button so I won't need to store it as a handle although I am not opposed to doing that. I am pretty sure what ever I am doing wrong is a simple mistake. I debugged the program and the error occurs on the line where I am trying to extract the information from N.

Started from scratch and it worked

must have edited the wrong thing thanks for everyone's help!!

Log in to comment.

Answer by 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


Eric thank you for your response.

I thought that was what the get function was essentially doing. I have only ever used the load function to load variable, picture, ect ... into my code that were already in the directory or store somewhere on my computer not a user inputted source. However, I did try it with load instead of get and got an error.

I see I'm wrong from the code sample. I always just debug/set a setpoint and see what the handles are. You are dereferencing the handles structure incorrectly somehow, only your debugger knows for sure. It will become obvious when you do this.

I can tell from the code that Steven is using GUIDE; GUIDE callbacks automatically pass the handles structure in.

Log in to comment.

Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

MATLAB Academy

New to MATLAB?

Learn MATLAB today!