Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
GUI edit onKeyPressed

Subject: GUI edit onKeyPressed

From: Igor

Date: 17 Jul, 2010 22:47:03

Message: 1 of 6

I'd like to know edit's value while edit is in progress(still has focus).
At first I've tried simple get(handle,'String') in @KeyPressFcn.
Yet, result contains the value edit had before it was edited....This value only 'refreshes' after focus was lost.

There was a tiny hope of 'Action' event (as with slider continuous callback)- this event never occurs.
So, this lead us to java… I’m a complete newbie about it… The only way I managed to do this somehow is using findjobj:
http://www.mathworks.com/matlabcentral/fileexchange/14317-findjobj-find-java-handles-of-matlab-graphic-objects
try
    [handles, levels, parentIds, listing] = findjobj(handle,'nomenu',...
        'class','edit',...
        'nomenu',...
        'depth',15);
    
    for i=1:length(handles)
        Uhandle=get(handles(i));
        
        
        jColor=Uhandle.Foreground;
        R=get(jColor,'Red');
        G=get(jColor,'Green');
        B=get(jColor,'Blue');
        
        
        
        if all([R G B]==uniq_color*256)
            Jobj=Uhandle;
            break;
        end
    end
catch
    Jobj=[];
end

here,
str=obj.Jobj.Text;
is the string we want. But all this doesn’t look like best way to access that property..
also
-“pure” findjobj(…) sometimes return numerous handles. Is there any thing we can be sure to be uniq for this object. Where is ‘tag’ stored?
Also, findobj is slow, it takes about half a second every time. And we need to update every KeyPress, Jobj.Text is not changing either…

Subject: GUI edit onKeyPressed

From: Yair Altman

Date: 18 Jul, 2010 05:24:03

Message: 2 of 6

"Igor " <i3v@mail.ru> wrote in message <i1tbt7$hmb$1@fred.mathworks.com>...
> I'd like to know edit's value while edit is in progress(still has focus).
[...]
> So, this lead us to java… I’m a complete newbie about it… The only way I managed to do this somehow is using findjobj:
> http://www.mathworks.com/matlabcentral/fileexchange/14317-findjobj-find-java-handles-of-matlab-graphic-objects
> try
> [handles, levels, parentIds, listing] = findjobj(handle,'nomenu',...
> 'class','edit',...
> 'nomenu',...
> 'depth',15);
[...]
> here,
> str=obj.Jobj.Text;
> is the string we want. But all this doesn’t look like best way to access that property..
> also
> -“pure” findjobj(…) sometimes return numerous handles. Is there any thing we can be sure to be uniq for this object. Where is ‘tag’ stored?
> Also, findobj is slow, it takes about half a second every time. And we need to update every KeyPress, Jobj.Text is not changing either…

The edit uicontrol does not expose its Java reference directly, so the only way to get it is using the findjobj utility.

FindJObj does not need to be called every time - you can use it to get the Java handle in your *_OutputFcn function, store this handle somewhere persistent (for example, in the handles struct) and then directly use this handle from that point onward.

Also, there is no need to pass the 'nomenu' argument twice to FindJObj and I also see no reason for using the 'depth' parameter. There is also no reason for all the output args - only the first (handles) is really needed in your case.

Yair Altman
http://UndocumentedMatlab.com

Subject: GUI edit onKeyPressed

From: Igor

Date: 18 Jul, 2010 13:41:06

Message: 3 of 6

"Yair Altman" <altmanyDEL@gmailDEL.comDEL> wrote in message <i1u35j$i81$1@fred.mathworks.com>...
>[…]
> FindJObj does not need to be called every time - you can use it to get the Java handle in your *_OutputFcn function, store this handle somewhere persistent (for example, in the handles struct) and then directly use this handle from that point onward.
>
> Also, there is no need to pass the 'nomenu' argument twice to FindJObj and I also see no reason for using the 'depth' parameter. There is also no reason for all the output args - only the first (handles) is really needed in your case.
>
> Yair Altman
> http://UndocumentedMatlab.com

Thanks for your answer,
Seems like my main dumb mistake was to store result of get(handles(i)) , instead of handles(i) itself, in my persistent class… that’s why I needed to use findobj every update. Well… it’s OK now.
 ‘nomenu’ twice and all output args are just results of ‘routh draft’ nature of those function… Yet ‘depth’ parameter seemed to increase performance… Now, after tic-toc test (down here) I can see it gives nothing.

The only little trouble now is ‘initialisation’ –where to perform those findobj – it returns nothing being used in ‘GUIopeningFunction’. There is no ‘post opening’ callback provided in matlab. So, by now, ‘findobj’ is performed just before first use of it’s result. May be this is not that bad, this way it won’t affect GUI loading time. Yet, is there any way to implement some ‘post-opening’ callback? I was not able to find something like that in handles output.

global GUI_handles
 
a=tic();
handles= findjobj(GUI_handles.UM_edit1,...
                                           'class','edit','nomenu','depth',20);
disp(['Single exec:' num2str(toc(a))] );
 
 
 
 
a=tic();
for i=1:10
handles = findjobj(GUI_handles.UM_edit1,...
                                           'class','edit','nomenu','depth',20);
end
disp(['10times exec:' num2str(toc(a))] );
 
a=tic();
for i=1:10
[handles, levels, parentIds, listing] = findjobj(GUI_handles.UM_edit1,...
                                           'class','edit','nomenu','depth',20);
end
disp(['10times-4 outputs exec:' num2str(toc(a))] );
 
 
a=tic();
for i=1:10
handles = findjobj(GUI_handles.UM_edit1,...
                                           'class','edit','nomenu','depth',100);
end
disp(['10times-depth=100 exec:' num2str(toc(a))] );
 
 
a=tic();
for i=1:10
handles = findjobj(GUI_handles.UM_edit1,...
                                           'class','edit','nomenu');
end
disp(['10times-depth=Inf exec:' num2str(toc(a))] );
output:
list_handles
Single exec:1.3854
10times exec:13.6792
10times-4 outputs exec:13.8726
10times-depth=100 exec:14.1215
10times-depth=Inf exec:13.8045

Subject: GUI edit onKeyPressed

From: Yair Altman

Date: 18 Jul, 2010 15:17:04

Message: 4 of 6

> The only little trouble now is ‘initialisation’ –where to perform those findobj – it returns nothing being used in ‘GUIopeningFunction’. There is no ‘post opening’ callback provided in matlab. So, by now, ‘findobj’ is performed just before first use of it’s result. May be this is not that bad, this way it won’t affect GUI loading time. Yet, is there any way to implement some ‘post-opening’ callback? I was not able to find something like that in handles output.


Java objects are only created when the GUI is actually displayed. In your GUI's _OpeningFcn, they are not yet created so FindJObj obviously cannot find them. Instead, place FindJObj in your _OutputFcn as I already mentioned above.

You don't need to keep the handle - simply set its KeyReleasedCallback or KeyTypedCallback to your Matlab callback function. From that point onward, you don't need to use findjobj ever again.

Yair Altman
http://UndocumentedMatlab.com

Subject: GUI edit onKeyPressed

From: Igor

Date: 18 Jul, 2010 18:48:03

Message: 5 of 6

"Yair Altman" <altmanyDEL@gmailDEL.comDEL> wrote in message <i1v5tg$5f9$1@fred.mathworks.com>...
> Java objects are only created when the GUI is actually displayed. In your GUI's _OpeningFcn, they are not yet created so FindJObj obviously cannot find them. Instead, place FindJObj in your _OutputFcn as I already mentioned above.
>
> You don't need to keep the handle - simply set its KeyReleasedCallback or KeyTypedCallback to your Matlab callback function. From that point onward, you don't need to use findjobj ever again.
>
> Yair Altman
> http://UndocumentedMatlab.com

Eventually, I did it this way. My previous idea was to initialize it along with over GUI elements, within class constructor placed in Opening Function. Well, the good thing is this doesn’t affect GUI loading time now.
Thanks for your help.

Subject: GUI edit onKeyPressed

From: Romesh

Date: 20 Jun, 2012 00:54:08

Message: 6 of 6

"Yair Altman" wrote in message <i1v5tg$5f9$1@fred.mathworks.com>...
> Java objects are only created when the GUI is actually displayed. In your GUI's _OpeningFcn, they are not yet created so FindJObj obviously cannot find them. Instead, place FindJObj in your _OutputFcn as I already mentioned above.

In case this helps anyone in future, I have a GUI with multiple sub-GUIs that correspond to settings pages...the settings pages use uiwait() in the OpeningFcn to halt execution, and uiresume() is run when the user clicks 'Apply' or 'Cancel'. Thus the OutputFcn deletes the figure and returns the new parameter values to the program. In this case, putting findjobj() in the OutputFcn isn't suitable.

I struggled with this for a long time, but if you set the figure visibility to 'On' in the OpeningFcn e.g.

set(handles.figure1,'Visible','on');

then it is rendered immediately during the OpeningFcn and you can use findjobj() together with uiwait(). Just as long as findjob() is run after you have set the Visible property.

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us