필터 지우기
필터 지우기

Programmatically trigger events for UI objects (AppDesigner) using "notify()"

조회 수: 20 (최근 30일)
Benoit Beaulieu
Benoit Beaulieu 2024년 3월 19일
댓글: Benoit Beaulieu 2024년 3월 27일
I'm having a hard time ant this almost feels like a bug.
I'm trying to trigger the "ChangedValue" event associated with an object (buy handle) while scanning through a collection (array) of objects, for every time the value of the object is programmatically changed.
Could someone explain why the following pseudo-code is not working?
% Prior code includes the creation of listeners such as changing the value from the GUI triggers the events correctly.
function changeEveryValues(~,collections)
for i=1:length(collection)
collection(i).Value = ADifferentValue(); % Just because the value needs to be changed
EveryPossibleEventNames = events(collection(i)) % This shows clearly the validity of the event name.
AValidEventName = EveryPossibleEventNames{1} % Prints 'ChangedValue'
notify(collection(i),AValidEventName); % TROWS ERROR
end
end
Every time, matlab trows an error such as:
Cannot notify listeners of event 'Name of the Event' in class 'Name of the class'.
This works, but changes nothing, else than giving a handle for the listener, which is useless.
lh = addlistener(collection(i),'ValueChanged',collection(i).ValueChangedFcn);
Need more details? Feel free to ask!
Any insight on how this can be done? I tried calling the callback function from the function handle but without success. Notifying an event was my workaround but is, yet again, a dead end.

답변 (1개)

Samay Sagar
Samay Sagar 2024년 3월 25일
The given error you are encountering is possibly because the event has been defined with private or protected “NotifyAccess”. Ensure that the event is public to notify the event from within the class itself. Also,ensure that each object in your collection has a listener attached that responds to the “ChangedValue event.
Here is how you can implement a MATLAB class with custom events:
classdef MyObject < handle
properties
Value
end
events
ChangedValue
end
methods
function obj = MyObject(initialValue)
obj.Value = initialValue;
end
function setValue(obj, newValue)
obj.Value = newValue;
end
end
end
Attaching a sample MLAPP that uses the “notify” function to trigger custom callbacks for your reference.
  댓글 수: 1
Benoit Beaulieu
Benoit Beaulieu 2024년 3월 27일
Thanks for the isight. Although I'm a little confused on how to apply this and I hope you can help.
The objects I'm using are generated by the app designer and cannot be of a custom type. Moreover these types cannot be superclass as they are sealed.
Error :
Class 'matlab.ui.control.Spinner' is Sealed and may not be used as a superclass.
How do you think we could make it work?
Thanks

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

카테고리

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

제품


릴리스

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by