MATLAB Answers

ValueChanged event not fired on uieditfield when setting property Value

48 views (last 30 days)
Andreas Klotzek
Andreas Klotzek on 10 Jan 2019
Commented: J. Alex Lee on 24 Apr 2020
Hi
I have a uieditfield:
u = uieditfield();
The I attach a listener to the ValueChangedFcn
u.ValueChangedFcn = @(s,e)disp(e);
If I type into the edit field the my listener gets called as expected. But if I set the property directly
u.Value = '2';
no event is fired.
Is there something wrong with what I am doing or with Matlab?

  4 Comments

Show 1 older comment
Andreas Klotzek
Andreas Klotzek on 10 Jan 2019
You are right. It is actually mentioned in the documentation. And it is also true for the old uicontrol.
But on the old uicontrol I am able to do this:
u = uicontrol( 'style', 'edit' );
f = @(s,e)disp(e);
u.Callback = f;
addlistener( u, 'String', 'PostSet', f );
In this case I will get notified in all relevant cases. On the new edit field I cannot do this:
u = uieditfield();
addlistener( u, 'Value', 'PostSet', f );
This will lead to an error because the Property Value is not SetObservable. But maybe this will change in a future release?
Adam
Adam on 10 Jan 2019
If you are familiar with object-oriented programming you could create a simple wrapper class around your uieditfield as an interface through which you access it programmatically, then within the class functions you can trigger events when the value is set. It can be done with just functions too I guess, but less easily.
I don't know if there is an alternative for uieditfield or whether it is planned to open up this functionality in future since it is still very much under development.
J. Alex Lee
J. Alex Lee on 24 Apr 2020
Is this a concious decision based on some programming principle? It seems to me the more obvious/expected behavior is that changing the "Value", no matter how, triggers the "ValueChangedFcn"...otherwise they should have called it the "UIElementManipulatedFcn"...
I understand the wrapper concept as suggested by Adam and demonstrated by Jan, but it seems a cumbersome solution especially if you want to ensure your event object looks the same as it does from the manipulation of the ui element, and you want to do this for many types of ui elements...
Am I missing something?

Sign in to comment.

Answers (1)

Jan
Jan on 11 Jan 2019
Edited: Jan on 11 Jan 2019
You can write a wrapper for setting the value programmatically, which calls the callback manually:
function SetMyValue(objectH, Value)
objectH.Value = Value;
myEvent.Origin = 'Value set programmatically';
objectH.ValueChangedFcn(objectH, myEvent);
end

  2 Comments

Andreas Klotzek
Andreas Klotzek on 11 Jan 2019
The handle is not my own resource but belongs to someone else. This means I cannot force users to only use such a function, or to use a wrapper object.
Or the other way round: I cannot prevent the property to be set directly by someone.

Sign in to comment.

Products


Release

R2018b

Community Treasure Hunt

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

Start Hunting!