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
0 个评论
采纳的回答
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.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Naming Conventions 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!