Get Code Description of Generated Code
You can use the code descriptor API to obtain metadata about the generated code. For each model build, the code generator, by default, creates a codedescriptor.dmr
file in the build folder. When simulating the model in Accelerator and Rapid Accelerator modes, the codedescriptor.dmr
is not generated.
You can use the code descriptor API once the code is generated. Use the code descriptor API to describe these items in the generated code:
Data Interfaces: inports, outports, parameters, data stores, and internal data.
Function Interfaces: initialize, output, update, and terminate.
Run-time information of the data and function interfaces, such as timing requirements of each interface entity.
Model hierarchy information and code description of referenced models.
Get Data Interface Information
The coder.descriptor.DataInterface
object describes various properties for a specified data interface in the generated code. In the model CustomCodeComments
, there are four inports, one outport, and a tunable external parameter. For more information about the data interfaces in your model, use the coder.codedescriptor.CodeDescriptor
object and its methods.
1. Open and build the model.
open_system('CustomCodeComments'); evalc('slbuild(''CustomCodeComments'')');
2. Create a coder.codedescriptor.CodeDescriptor
object for the required model by using the getCodeDescriptor
function.
codeDescriptor = coder.getCodeDescriptor('CustomCodeComments');
3. To obtain a list of all the data interface types in the generated code, use the getDataInterfaceTypes
method.
dataInterfaceTypes = codeDescriptor.getDataInterfaceTypes()
dataInterfaceTypes = 5x1 cell array {'Inports' } {'Outports' } {'Parameters' } {'ExternalParameterObjects'} {'InternalData' }
To obtain a list of all the supported data interfaces, use the getAllDataInterfaceTypes
method.
4. To obtain more information about a particular data interface type, use the getDataInterfaces
method.
dataInterface = codeDescriptor.getDataInterfaces('Inports');
This method returns properties of the Inport blocks in the generated code.
5. Because this model has four inports, dataInterface
is an array of coder.descriptor.DataInterface
objects. Obtain the details of the first Inport of the model by accessing the first location in the array.
dataInterface(1)
ans = DataInterface with properties: Type: [1x1 coder.descriptor.types.Type] SID: 'CustomCodeComments:99' GraphicalName: 'In1' VariantInfo: [1x0 coder.descriptor.VariantInfo] Implementation: [1x1 coder.descriptor.DataImplementation] Timing: [1x1 coder.descriptor.TimingInterface] Unit: '' Range: [1x0 coder.descriptor.Range]
Get Function Interface Information
The function interfaces are the entry-point functions in the generated code. In the model RollAxisAutopilot
, the entry-point functions are model_initialize
, model_step
, and model_terminate
. For more information about the function interfaces in your model, use the coder.descriptor.FunctionInterface
object.
1. To avoid a build error, remove the slprj directory from the previous build. Open and build the RollAxisAutopilot
model.
rmdir ('slprj','s') open_system('RollAxisAutopilot') slbuild('RollAxisAutopilot')
### Starting build procedure for: RollAxisAutopilot ### Successful completion of build procedure for: RollAxisAutopilot Build Summary Top model targets: Model Build Reason Status Build Duration ==================================================================================================================== RollAxisAutopilot Information cache folder or artifacts were missing. Code generated and compiled. 0h 0m 15.571s 1 of 1 models built (0 models already up to date) Build duration: 0h 0m 17.014s
2. Create a coder.codedescriptor.CodeDescriptor
object for the required model by using the getCodeDescriptor
function.
codeDescriptor = coder.getCodeDescriptor('RollAxisAutopilot');
3. To obtain a list of all the function interface types in the generated code, use the getFunctionInterfaceTypes
method.
functionInterfaceTypes = codeDescriptor.getFunctionInterfaceTypes()
functionInterfaceTypes = 2x1 cell array {'Initialize'} {'Output' }
To obtain a list of all the supported function interfaces, use the getAllFunctionInterfaceTypes
method.
4. To obtain more information about a particular function interface type, use the getFunctionInterfaces
method.
functionInterface = codeDescriptor.getFunctionInterfaces('Initialize')
functionInterface = FunctionInterface with properties: Prototype: [1x1 coder.descriptor.types.Prototype] ActualReturn: [1x0 coder.descriptor.DataInterface] VariantInfo: [1x0 coder.descriptor.VariantInfo] FunctionOwner: [1x0 coder.descriptor.TypedRegion] Timing: [1x1 coder.descriptor.TimingInterface] ActualArgs: [1x0 coder.descriptor.DataInterface Sequence]
5. You can further expand on the properties to obtain detailed information. To get the function return value, name, and arguments:
functionInterface.Prototype
% 6.
ans = Prototype with properties: Name: 'RollAxisAutopilot_initialize' Return: [1x0 coder.descriptor.types.Argument] HeaderFile: 'RollAxisAutopilot.h' SourceFile: 'RollAxisAutopilot.c' Arguments: [1x0 coder.descriptor.types.Argument Sequence]
Get Model Hierarchy Information
Use the coder.codedescriptor.CodeDescriptor
object to get the entire model hierarchy information. The model AsynchronousEventsTop
has model AsynchronousEventsRef
as the referenced model.
1. Open and build the model.
open_system('AsynchronousEventsTop'); evalc('slbuild(''AsynchronousEventsTop'')');
2. Create a coder.codedescriptor.CodeDescriptor
object for the required model by using the getCodeDescriptor
function.
codeDescriptor = coder.getCodeDescriptor('AsynchronousEventsTop');
3. Get a list of all the referenced models by using the getReferencedModelNames
method.
refModels = codeDescriptor.getReferencedModelNames()
refModels = 1x1 cell array {'AsynchronousEventsRef'}
4. To obtain the coder.codedescriptor.CodeDescriptor
object for the referenced model, use the getReferencedModelCodeDescriptor
method.
refCodeDescriptor = codeDescriptor.getReferencedModelCodeDescriptor('AsynchronousEventsRef');
You can now use the refCodeDescriptor
object to obtain more information about the referenced model by using all the available methods in the Code Descriptor API.