Hi,
In MATLAB, the ‘notify’ function is indeed designed to work with user-defined classes that inherit from the ‘handle’ class. When you try to use notify with built-in MATLAB classes or UI components like ‘uibutton’, you're limited to the events that those classes or components explicitly define and support. Unfortunately, this means you cannot directly trigger custom events like 'PropertyAdded' on MATLAB graphics handle objects such as ‘uibutton’ without extending those classes in some way.
The error you're encountering is because the ‘uibutton’ and the ‘uifigure’ does not have an event named 'PropertyAdded' defined, and MATLAB's built-in classes do not support dynamically adding events in the same way you might add properties or methods.
While you cannot directly use notify with built-in MATLAB UI components for custom events without extending those components in some way, creating wrapper classes or custom components is a way to add custom behaviour and events to MATLAB GUI applications. Here is an example of how you could create a custom class that wraps a ‘uibutton’ and includes an event for when a custom property is added:
classdef CustomButton < handle
Button matlab.ui.control.Button
CustomProperties containers.Map
function obj = CustomButton(parent)
obj.Button = uibutton(parent);
obj.CustomProperties = containers.Map;
function addCustomProperty(obj, propName, propValue)
obj.CustomProperties(propName) = propValue;
notify(obj, 'PropertyAdded');
You can now use this class in your GUI:
>> btn = CustomButton(fig);
>> addlistener(btn, 'PropertyAdded', @(varargin) disp('Custom Property added'));
>> btn.addCustomProperty('propertyName', 'value');
Hope this helps!