How to exclude events property when executing MATLAB coder
7 views (last 30 days)
Show older comments
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
0 Comments
Accepted Answer
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.
More Answers (0)
See Also
Categories
Find more on Naming Conventions 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!