How to pass variable to function handle using registerevent?
5 次查看(过去 30 天)
显示 更早的评论
I'm streaming data from another peice of software and I'm having matlab read events from the software and if and event occurs I call my function. The funciton handler requires me to specifcy which channel the event will occur, but I'm not clear on the syntact I should use to specificy.
Below is what I have, where I need some variables to pass into 'OnEventDataArrived"
global gLCDoc
gLCDoc.registerevent({'OnEventDataArrived' @app.MyFunction})
Does anyone have any ideas?
0 个评论
回答(1 个)
Hari
2023-10-3
编辑:Hari
2023-10-3
Hi Dc215905,
I understand that you are streaming data from another software and want to read events in MATLAB. When an event occurs, you are calling a function that requires you to specify which channel the event will occur, and you want to know how to pass variables to a function handle using the “registerevent” function in MATLAB.
To pass variables to the function handle using “registerevent”, you can use an anonymous function. Here's an example:
% Create an instance of the sample class
gLCDoc = SampleClass();
% Register the event and pass variables to the function handle
channel = 2; % Specify the channel you want to pass as a variable
eventData = struct(); c
gLCDoc.registerevent(eventData, @(src, event) MyFunction(src, event, channel)); % Register event is specified using anonymous function.
You can define a custom function to handle when the specific event is triggered.
function MyFunction(src, event, channel)
disp(['Event occurred on channel ' num2str(channel)]);
disp('Performing event handling...');
% Add your event handling code here
end
Here is the custom class to handle the events.
% Create a custom event data class
classdef MyEventData < event.EventData
properties
eventData
end
methods
function obj = MyEventData(eventData)
obj.eventData = eventData;
end
end
end
% Create a sample handle class
classdef SampleClass < handle
events
OnEventDataArrived
end
methods
function registerevent(obj, eventData, functionHandle)
notify(obj, 'OnEventDataArrived', MyEventData(eventData));
obj.addlistener('OnEventDataArrived', functionHandle);
end
end
end
You can simulate the behaviour using “notify” function by virtually triggering the event.
% Simulate an event occurrence
notify(gLCDoc, 'OnEventDataArrived');
Here is the output observed:
Event occurred on channel 2
Performing event handling...
By doing this way you can trigger an event by passing arguments to the custom function and handle the event triggering workflow.
Refer to the MATLAB documentation for Anonymous Functions to learn more about creating anonymous functions in MATLAB.
For more information on the “registerevent” method, you can refer to the MATLAB documentation on event notification and handling.
Hope this helps!
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Environment and Settings 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!