How to exclude events property when executing MATLAB coder

12 次查看(过去 30 天)
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

采纳的回答

Denis Gurchenkov
Denis Gurchenkov 2023-9-18
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 个评论
Kritsada
Kritsada 2023-9-20
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
% --------------------------------------------------------------------

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Naming Conventions 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by