How to implement arrays of events and listeners
4 views (last 30 days)
I would like to be able to apply the event - listener methodology to obtain notification when specified, individual elements of an array of values are changed. It seems that there is no built in support for arrays of event, the events for a class are just specified by individual names. If anyone has an idea for a good way to have a listener be notified when an individual array element (property of the class) is changed I would appreciate it. Additional details, for context are below.
The situation is the following.
I am implementing a prototype of a practical industrial control system using MATLAB.
I have multiple identical machines identified by their machine number 1,2,3... numMachines. Each machine is equipped with sensors at identical locations identified by sensor ID's 1,2, ... numSensors. So one can imagine the sensor readings, e.g. temperature values, being held in a numSensor by numMachine array.
The individual sensor values are updated (changed) occassionaly at unpredictable (non-periodic) intervals.
Each machine has numControllers controllers. Each of these controllers is interested in a particular subset of the sensor values. The controllers for each machine are identical. So for example the controller 2 for machine 3 might be interested in the measurements for sensor 2,8 and 11 of machine 3. Controller 2 for any other machine will be interested in the same set of sensor values but for the corresponding machine.
So what I would like to do (functionally), is have each controller listen for changes to the sensor values it is interested in. That is, changes in a particular machine number and sensor number. On the other side, when ever a value for a particular machine and sensor changes a notification should be made by the corresponding event.
The basic capability to do this seems to be available with MATLAB event-listener mechanism. The difficulty I am having is how to implement this for an array of events (e.g. numSensor by numMachine array). Typically 30 sensors by 12 machines. I only see the ability to define events by individual names, e.g. event1, event2 etc. I don't see anyway to define an array of events for this purpose. Of course I could include the machine number and sensor id in the event names, for example event_m03s29 for machine 3 sensor 29. This seems like generally bad practice, and would at the least be very tedious to code for the dimensions of intererst.
As an alternative, I could just blast out an event notification to all of the controllers, with a subclassed event data to pass the machine id and sensor id, and value for the changed value. Then each controller could look and see if it were interested. It seems inefficient though, to have everyone receive notifications, which usually are not of interest.
If anyone has suggestion on a good way to implement the functionality I have described I would appreciate any ideas.
Manoj Mirge on 17 Aug 2023
The array of event listeners in MATLAB can be implemented using dynamic properties on the instance of class. You may create the instance of class and add the different attributes representing the sensors of machine programmatically.
Please refer to the attached link to add dynamic properties to the class:
Then you can set the “SetObservable” property of these dyanamic properties as “true” and attach the listeners for the “PostSet” event for the dynamic properties.
Kindly refer to the below code to create and attach listeners to dynamic properties in MATLAB:
classdef MyClass < dynamicprops
function obj = first(NumberOfMachines,NumberOfSensors)
% The value of sensor 1 of Machine 1 has been changed
% The value of sensor 2 of Machine 1 has been changed
% So on for rest of the properties
Hope this helps.