MATLAB Answers


GUIDE: "Submitting" values in text boxes

Asked by David
on 6 Nov 2013
Latest activity Commented on by Sean de Wolski
on 22 Nov 2013

I'm familiar with code that can take an object input into a text box in a GUI and retrieve it for later use. However, it seems to only do this explicitly if either a) the box is initialized with a value, or b) after entering the text, the user hits Enter or clicks anywhere on the GUI EXCEPT for on a button. It's this last extreme case I want to address; let's face it, sometimes you put something in and push the "do stuff" button immediately. But no matter what I do, I cannot read the contents of that box if I push the button as soon as I stop typing.

Here's some example code:

function editbox_Callback(hObject, eventdata, handles)
global globalvar1 globalvar2
% hObject    handle to animal_editbox (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 animal_editbox as text
%        str2double(get(hObject,'String')) returns contents of animal_editbox as a double
save 'globalvars1.mat' globalvar1;
% Just in case
handles.textString = fnin;
guidata(hObject, handles);

With this, if I hit enter after typing the text, or if I click on the GUI, both globalvar1.textvar and handles.textString are updated to the new value in the box. However, if I were to click on a button that loads another routine, it uses whatever the previously submitted string was, and updates these variables AFTER all the other stuff is run. I've tried doing


but that also gives me the previous version; the values are not "updated" unless I hit enter or click the GUI anywhere but a button.

Is there any way around this that doesn't involve telling the user to hit enter when he's done typing? Thanks.

EDIT: This seems to be an error on my machine, which is running MATLAB 6.5 on Windows 8; on Windows XP with MATLAB 6.5 it behaves as it should. There seems to be an issue with inputs in general not being "accepted" unless I hit enter (there's a similar problem with inputdlg - I get an empty result when I just hit OK to accept the input).

  1 Comment

Well MATLAB 6.5 was released about 10 years before Windows 8 and is certainly not supported for it.

You'll need to either run MATLAB 6.5 on an old machine or upgrade MATLAB to a newer release compatible with Windows 8.

Log in to comment.


1 Answer

Answer by Image Analyst
on 6 Nov 2013

I do not observe that. I made a gui with an edit box that was empty. Then I made a button. In the button callback I had

editTextContents = get(handles.edtEditText, 'String');

and it accurately retrieved the value in the edit box as soon as I clicked the button immediately after typing something into the edit box.


Have you tried this with inputdlg as well? Because when I just type something like

foo = inputdlg('blah','blah')

I can type something into the box, press "OK", and foo will come back empty.

For me it does not come back empty:

>> foo = inputdlg('blah','blah')
foo = 
    'my input'

It returned exactly what I typed. Your installation sounds seriously messed up. You might want to reinstall, or upgrade to the latest version, or call tech support for help.

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!