フィルターのクリア

GUIDE: missing data in handles

6 ビュー (過去 30 日間)
Rob Campbell
Rob Campbell 2017 年 9 月 19 日
編集済み: OCDER 2017 年 9 月 20 日
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 件のコメント
Walter Roberson
Walter Roberson 2017 年 9 月 19 日
handles has already been passed in, so you should not need to
handles = guidata(hObject);
but it should not hurt either.
Rob Campbell
Rob Campbell 2017 年 9 月 20 日
I figured it out, it was a timing issue. For whatever reason, the text in the edit box did not register immediately upon hitting enter, and so calling "get(hObject,'String')" returned an empty array. Setting a breakpoint gave it enough time to catch up and it would register. I was able to get it to work by adding a pause(0.01) prior to
handles.id(handles.index).keypress=get(hObject,'String');

サインインしてコメントする。

採用された回答

OCDER
OCDER 2017 年 9 月 19 日
編集済み: OCDER 2017 年 9 月 20 日
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);
  2 件のコメント
Rob Campbell
Rob Campbell 2017 年 9 月 20 日
That's got it, thanks! I figured out a workaround with a short pause (comment above), but this is much more elegant.
OCDER
OCDER 2017 年 9 月 20 日
Ah, that's an interesting update issue. Glad you found the fix!
You can also use drawnow instead of pause(0.01), which saves you from having to adjust 0.01 pause time for slower computers.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeInteractive Control and Callbacks についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by