GUIDE: missing data in handles
Show older comments
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
3 Comments
Rik
on 19 Sep 2017
A change of behavior between normal running and running with breakpoints generally only happens if there are graphics involved, so this intrigues me.
A point of clarification: guidata saves a variable (which you should always make a struct for flexible use) to a graphics object (commonly the main figure of your GUI). You save the data if you call both the handle and the struct, and you load the data by only calling the handle. That last thing is automatically included in a GUIDE-generated callback function. (so that first call is not necessary)
Another note: I always use strcmp for comparing strings, but I don't know what is best practice.
Walter Roberson
on 19 Sep 2017
handles has already been passed in, so you should not need to
handles = guidata(hObject);
but it should not hurt either.
Rob Campbell
on 20 Sep 2017
Accepted Answer
More Answers (0)
Categories
Find more on Interactive Control and Callbacks in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!