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
fnin=get(hObject,'String'); globalvar1.textvar=fnin; 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).
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.