How to exclude events property when executing MATLAB coder
조회 수: 5 (최근 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!