Effacer les filtres
Effacer les filtres

ValueChangedFcn on uieditfield (uifigure based app)

10 vues (au cours des 30 derniers jours)
Antonio Hortal
Antonio Hortal le 24 Jan 2021
Hi everyone
I am trying to implement a search bar in a uifigure based app, for which I am using a uieditfield. The behaviour that I want to achieve is that when I press enter while on the uieditfield, a function is called that takes the value of edit field and does something with it. Using the ValueChangedFcn property of the editfield looked initially like the way to go, but I am finding some issues along the way.
The documentation says that the ValueChangedFcn callback "executes when the user changes text in the edit field and either presses Enter or clicks outside the edit field". Two points outstand from this:
  • I want only the search to start on an 'Enter' keypress, and not by clicking outside the searchbar
  • If the text has not changed and the user presses the enter button, the function should trigger anyways. Imagine the case in which the user is typing on the searchbar, changes focus to another component (to view an axes for example) and then goes back to the searbar and presses enter to search what he had previously written.
My initial idea was something easy like:
% searchbarValueChangedCallback is the function linked to the ValueChangedFcn of the editfield
function searchbarValueChangedCallback(src,evt)
% src is the uieditfield, and evt is the event passed by the ValueChangedFcn
fH = ancestor(src,'figure'); % Get the figure handle
key = get(fH,'CurrentKey'); % Get the last pressed key
switch key
case 'return' % user pressed enter
% Execute the search function
disp('Execute the search function :D')
otherwise % user clicked outside the editfield.
% Dont do anything
If the user presses the Enter key, the function is triggered (in this case, the disp()). If the user clicks outside, nothing happens. So far, so good. But what happens now if the user clicks outside and then reenters the editfield and presses Enter? Nothing because the ValueChangedFcn is not executed if the Value of the editfield has not changed. This is where the problem lies...
I could add a WindowKeyPressFcn to the ancestor figure that checks for an Enter, but that would trigger even if the editfield is not being focused. And, as far as I know, there is currently no way to detect the component being currently focused on Matlab 2020b (>>gco does not work for uifigure based apps and the hidden property of the uifigure 'CurrentObject' is always empty).
All in all, I find the following 2 issues blocking me:
  1. The ValueChangedFcn is only triggered if the Value property of the edit field has changed. I understand the logics behind this (a value that has not changed does not trigger a ValueChanged listener) but it would be great if there was KeyPressFcn callback on the editfield to register keys even if the Value doesnt change.
  2. There is no way to detect the component on focus. gco would be the ideal solution but its not there yet (and it looks like in 2021a it wont arrive either)
Does anybody have an alternative solution? Or do you think I have reached a limitation that can't be bypassed?. I have tried going the undocumented way but I have not managed to find a solution
Thanks to anybody that can help :)

Réponse acceptée

Vimal Rathod
Vimal Rathod le 1 Fév 2021
Modifié(e) : Vimal Rathod le 1 Fév 2021
As you have tried all the possible solutions, one possible workaround is to add a manual search button which the user could click to do search. Other solution is to try using "CurrentObject" property of uifigure in R2021a release. The R2021a release has added components like "uieditfield" to be the "CurrentObject" and hence now the uifigure current object won't be empty. So if you update MATLAB, then you could use the "CurrentObject" property of uifigure and uieditfield will show up when in focus and based on that you could trigger your function.
Hope this helps!
  1 commentaire
Antonio Hortal
Antonio Hortal le 1 Fév 2021
Thanks for the answer Vimal. It is unfortunate that the feature is only available in 2021a, but good that at least it is arriving :)

Connectez-vous pour commenter.

Plus de réponses (1)

Jaber EL HADAD le 7 Avr 2021
When I right click on callbacs I can't find AddValueChangedFcn I have only ButtonPushedFcn I have the R2018b version of Matlab can you help plesease?


En savoir plus sur Develop uifigure-Based Apps dans Help Center et File Exchange




Community Treasure Hunt

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

Start Hunting!

Translated by