MATLAB: Is the custom header file not included correctly in the code generated from an Embedded MATLAB Function subsystem using Real-Time Workshop Embedded Coder 5.3 (R2009a)

Embedded Coder

I have a model containing an Embedded MATLAB Function block that uses an intermediate structure variable, whose type is defined in an external header file called "mytypes.h". The eml.cstructname directive is used with the 'extern' parameter to achieve this, and the custom header file is included in the 'Simulation Target->Custom Code' and 'Real-Time Workshop->Custom Code' panes of the Configuration Parameters window.
When I build this subsystem as a reusable function with a user-specified name, the code generated from it does not compile. The compiler errors occur because the externally defined C structure is not recognized, due to the fact that Real-Time Workshop does not include my custom header file in the generated code before it is used.
In order to avoid the compiler error, Real-Time Workshop Embedded Coder must include my custom header file in <model>_types.h and not <model>.h.

Best Answer

This bug has been fixed in Release 2011b (R2011b). For previous product releases, read below for any possible workarounds:
The ability to automatically deduce that a custom header file specified in the 'Custom Code' panes of the Configuration Parameters window must be included in the <model>_types.h file (since it defines types used in the Embedded MATLAB Function block) instead of the <model>.h file is not available in Real-Time Workshop Embedded Coder 5.3 (R2009a) . Externally defined types are specified using data objects like Simulink.Signal and Simulink.Bus associated with Simulink.AliasType objects which places the corresponding header file in the <model>_types.h file.
In order to work around this limitation, implement one of the following solutions:
1) Process the generated <model>_types.h file using a Custom File Processing template, so that the line:
#include "mytypes.h"
is added deliberately.
This can be done by specifying a file customization template in the 'Real-Time Workshop->Templates' pane, which is defined as follows:
%selectfile NULL_FILE
%assign mdlName = LibGetModelName() + "_types"
%assign modelH = LibCreateSourceFile("Header", "Simulink", mdlName)
%openfile tmpBuf
#include "mytypes.h"
%closefile tmpBuf
%<LibSetSourceFileSection(modelH,"Includes",tmpBuf)>
Refer to the following page for more information on the capabilities of the Custom File Processing templates option:
2) Use a Simulink.AliasType object to introduce a "dummy alias type", such that mytypes.h is indirectly introduced into <model>_types.h.
If you are unfamiliar with Simulink data objects, refer to the following documentation page:
Introduce a dummy Simulink.AliasType object called 'Alias' with the HeaderFile attribute set to "mytypes.h", and a Simulink.Signal object that resolves to the input signal to the Embedded MATLAB Function block of type 'Alias' as follows:
Alias = Simulink.AliasType;
Alias.HeaderFile = 'mytypes.h';
Alias.BaseType = 'double'
in = Simulink.Signal;
in.DataType = 'Alias';
Note that this Simulink.Signal object can be associated with any signal that has the same BaseType as the Simulink.AliasType object. This forces "mytypes.h" to be included in <model>_types.h.