How to exclude events property when executing MATLAB coder

5 views (last 30 days)
I've implemented a feature using a class method and events properties for unit testing purposes.
I'm aware that MATLAB coder doesn't support events. and typically, I use the coder.target('MATLAB') condition to exclude certain MATLAB code. However, this approach doesn't work for events.
Currently, I'm manually commenting out the events when generating code and then uncommenting them when running unit tests. Do you have any suggestions on how to handle this situation more efficiently, without the need for manual comment/uncomment?"
function modelA < handle
events
debugEvent % Comment this line when do code generation.
end
method
% Some public method
end
method (Access=private)
function obj = notifyDebug(obj)
if coder.targer('MATLAB') % Exclude below code when executing code generation.
evtData = packEvtData();
notify(obj, debugEvent, evtData);
end
end
end
end

Accepted Answer

Denis Gurchenkov
Denis Gurchenkov on 18 Sep 2023
Hi Kristada, thanks for posting this question!
Ideally, MATLAB Coder should support events. At the very minimum, it should let you generate code for the part of your class that does not use events (as you do), simply ignoring the events declaration. I created a request for the development team to add this capability in the next release.
That being said, the only workaround as of now is to edit your code so that the events block is in some other class. Either use inheritance, so that for code generation you use some super-class (subModelCodegen) that has no events, and for MATLAB execution you inherit modelA from subModelCodegen. Or create another class that has events, and make modelA has a property that contains an object of that class with events.
Someone also suggested simply commenting out the events block programmatically before starting codegen command, and then un-commenting it back (via search and replace). This is a hack, but if you only have one such class, maybe such a hack is ok.
We are going to escalate this request to the development team so that suchh workarounds become unnecessary in the future.
  1 Comment
Kritsada
Kritsada on 20 Sep 2023
Many thanks for your suggestion. I have revised my code following your advice, and it now works perfectly. I no longer need to comment/uncomment anymore.
Here is an sample of my current source code.
%% main.m
% Caller function
function main
aClass = modelA;
if coder.target('MATLAB')
aClass.dbg = modelA_debugEvent;
lh = addlistener(aClass.dbg, 'debugEvent', @handleDebugEvent)
end
% Do something
end
function handleDebugEvent(obj, event)
% Do something
end
% --------------------------------------------------------------------
%% modelA.m
classdef modelA < handle
properties
dbg
end
methods
function obg = foobar(obj)
% Do something
if coder.target('MATLAB')
obj.dbg.notifyDebugEvent(param);
end
end
end
end
% --------------------------------------------------------------------
%% modelA_debugEvent.m
classdef modelA_debugEvent < handle
events
debugEvent
end
methods (Access=public)
function obj = notifyDebugEvent(obj, tbl)
if coder.target('MATLAB')
evtData = debugParam(tbl);
notify(obj, 'debugEvent', evtData);
end
end
end
end
% --------------------------------------------------------------------
%% debugParam.m
classdef (ConstructOnLoad) debugParam < event.EventData
properties
param
end
methods
function data = debugParam(tbl)
data.param = tbl;
end
end
end
% --------------------------------------------------------------------

Sign in to comment.

More Answers (0)

Categories

Find more on MATLAB Coder in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!