MATLAB: GUIDE: missing data in handles

callbackguidehandles

Hi: I made a little GUI to classify images in a directory. It shows the user the images, and there is an edit box in which the user may type what the image is, after hitting enter, the text in the box is supposed to be saved to handles. The contents of the edit box are saved to handles thusly:
function count_KeyPressFcn(hObject, eventdata, handles)
% hObject handle to count (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA
handles = guidata(hObject);
keypress=get(handles.figure1,'currentcharacter');
if isequal(keypress,char(13)) % Enter key is pressed
handles.id(handles.index).keypress=get(hObject,'String'); %get contents of edit box
handles.index=handles.index+1; % indexing for files - so to open next file in list.
guidata(hObject, handles);
idgui_showim(hObject, eventdata, handles); %This shows the next picture and empties out the edit box.
end
It'll cycle through the images just fine, but when the callback to save it is triggered (with a save button), the handles.id.keypress structure is empty (it is initialized to be an empty char array with the same length as the number of files in the directory). The handles.index value is correct though (e.g. if I look at 8 images it'll read 8).
I added the handles = guidata(hObject); line after searching around and thinking that I was not getting the updated handles, but that doesn't seem to be the problem.
The truly baffling thing is that if I set a breakpoint on the "handles.id(handles.index).keypress=get(hObject,'String');" line I can see the text I've typed showing up in handles.id.keypress as I cycle through, and then when I hit the save button, handles.id.keypress is not empty – everything is where it should be. But if I remove that breakpoint, then handles.id.keypress is always empty when the save button callback is triggered.
I'm quite confused at this point. Anyone have any thoughts on what's happening?
Thanks, Rob

Best Answer

Based on what I've tested with your code, get(hObject,'String') will NOT get the current string in the edit text box, but the prior string. I'm guessing this is because KeyPressFcn is summoned the moment you pressed the editable text box, meaning it passed the handles for the "current" state (before the edit text box string was updated). You could use drawnow to update the figure / GUI.
Since you are resetting the text back to empty (via idgui_showim ), the editable text box will only save empty strings to handles.id(handles.index).keypress.
To work around this, place the function after the if statement into the the edit textbox's Callback function instead of KeyPressFcn. The Callback should be summoned when the user presses enter or leaves the edit text box, and it uses the current string in the edit text box.
Hope this works!
%Use the edit textbox callback function (assuming the tag is "count")
function count_Callback(hObject, eventdata, handles)
handles.id(handles.index).keypress=get(hObject,'String'); %get contents of edit box
handles.index=handles.index+1; % indexing for files - so to open next file in list.
guidata(hObject, handles);
idgui_showim(hObject, eventdata, handles);