필터 지우기
필터 지우기

Forcing 'String' Property to update during a KeyPressFcn callback in a uicontrol(​'Style','e​dit',... object

조회 수: 5 (최근 30일)
Anyone know how to force an update to the 'String' property of an edit field as the user is typing characters, but before hitting 'enter'? I've tried refocusing uicontrol to another gui component, but that doesn't work for some reason, unless my implementation is wrong. Thanks,
-J
Here's how I change uicontrol focus. I've tried using other gui elements other than the figure.
% KeyPressFcn callback
function cb_KeyPressFcn(hObj,cKey)
hf = get(hObj,'Parent');
uicontrol(hf)
% some code
uicontrol(hObj)
get(hObj,'String') % but this outputs as empty

답변 (1개)

Jesse Hopkins
Jesse Hopkins 2013년 10월 18일
I've seen a couple solutions to this. See this answer for a solution using a java robot to push enter for you during the keypress callback, thus updating the string. This has a side effect of also firing the "Callback" function as well.
I've personally used findjobj.m on the file-exchange to gain access to the java object behind the edit control, and used the "KeyTypedCallback", which does provide an updated string.
I don't have the code in front of me now, but it looks something like this:
% Create the edit ui control
h_edit = uicontrol('style','edit');
%find the java object
jObj = findjobj(h_edit,'nomenu');
%set the keytypedcallback
set(jObj,'KeyTypedCallback',@myKeyTypedCallbackFcn)
The toggling the focus method used to work way back in R14. I did submit a bug report when I noticed that behavior change in R2007, and I also submitted a feature request a couple years ago that MW supply an updated string as part of the keypressfcn eventdata. MW replied that they are investigating implementing this functionality in a future release.
  댓글 수: 2
Jeff
Jeff 2013년 10월 28일
I get an error when I use the second line of your code:
>> jObj = findjobj(h_edit,'nomenu');
Undefined function 'findjobj' for input arguments of type 'double'.
I have MATLAB Version 7.13.0.564 (R2011b) Is there another command I can use? Or is this only for the most recent versions of Matlab?

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Migrate GUIDE Apps에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by