Get Code Description of Generated Code - MATLAB & Simulink (original) (raw)
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 =
5×1 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.
ans =
DataInterface with properties: Type: [1×1 coder.descriptor.types.Type] SID: 'CustomCodeComments:99' GraphicalName: 'In1' VariantInfo: [1×0 coder.descriptor.VariantInfo] Implementation: [1×1 coder.descriptor.DataImplementation] Timing: [1×1 coder.descriptor.TimingInterface] Unit: '' Range: [1×0 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')
Searching for referenced models in model 'RollAxisAutopilot'.
Total of 1 models to build.
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 9.6396s
1 of 1 models built (0 models already up to date) Build duration: 0h 0m 10.15s
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 =
2×1 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: [1×1 coder.descriptor.types.Prototype] ActualReturn: [1×0 coder.descriptor.DataInterface] VariantInfo: [1×0 coder.descriptor.VariantInfo] FunctionOwner: [1×0 coder.descriptor.TypedRegion] Timing: [1×1 coder.descriptor.TimingInterface] ActualArgs: [1×0 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: [1×0 coder.descriptor.types.Argument] HeaderFile: 'RollAxisAutopilot.h' SourceFile: 'RollAxisAutopilot.c' Arguments: [1×0 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 =
1×1 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.