ValueChanged event not fired on uieditfield when setting property Value

86 views (last 30 days)
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
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 (2)

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
  9 Comments
shakedpa
shakedpa on 10 Oct 2022
The original suggestion by Jan works fine, so that's ok for me. I felt there was a "more correct" way of doing it, but if there isn't anything obvious - nevermind.

Sign in to comment.


Alexander Cranney
Alexander Cranney on 15 Nov 2021
Edited: Alexander Cranney on 15 Nov 2021
Another way to get the behavior you want is (mis)use a test case. Test cases have a number of methods for modifying GUI values in ways that mimic having a user manipulate the values, which fires all of the ChangedFcns. In this case, you can use the "type" method:
u = uieditfield();
u.ValueChangedFcn = @(s,e)disp(e);
fakeTestCase = matlab.uitest.TestCase.forInteractiveUse;
fakeTestCase.type(u,'2')
  1 Comment
Alexander Cranney
Alexander Cranney on 15 Nov 2021
Ah, looks like you can do it in a single line without instantiating "fakeTestCase":
matlab.uitest.TestCase.forInteractiveUse.type(u,'2');

Sign in to comment.

Categories

Find more on Migrate GUIDE Apps in Help Center and File Exchange

Products


Release

R2018b

Community Treasure Hunt

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

Start Hunting!