Main Content

Get Code Description of Generated Code

You can use the code descriptor API to obtain meta-information 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 rtwdemo_comments, 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. Create a temporary folder for the build and inspection process.

currentDir = pwd;
[~,cgDir] = rtwdemodir();

2. Open and build the model.

open_system('rtwdemo_comments');
evalc('slbuild(''rtwdemo_comments'')');

3. Create a coder.codedescriptor.CodeDescriptor object for the required model by using the getCodeDescriptor function.

codeDescriptor = coder.getCodeDescriptor('rtwdemo_comments');

4. 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.

5. 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.

6. 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: 'rtwdemo_comments: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 rtwdemo_roll, 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. Create a temporary folder for the build and inspection process.

currentDir = pwd;
[~,cgDir] = rtwdemodir();

2. Open and build the model.

open_system('rtwdemo_roll');
evalc('slbuild(''rtwdemo_roll'')');

3. Create a coder.codedescriptor.CodeDescriptor object for the required model by using the getCodeDescriptor function.

codeDescriptor = coder.getCodeDescriptor('rtwdemo_roll');

4. 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.

5. 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]

6. You can further expand on the properties to obtain detailed information. To get the function return value, name, and arguments:

functionInterface.Prototype
ans = 

  Prototype with properties:
          Name: 'rtwdemo_roll_initialize'
        Return: [1x0 coder.descriptor.types.Argument]
    HeaderFile: 'rtwdemo_roll.h'
    SourceFile: 'rtwdemo_roll.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 rtwdemo_async_mdlreftop has model rtwdemo_async_mdlrefbot as the referenced model.

1. Create a temporary folder for the build and inspection process.

currentDir = pwd;
[~,cgDir] = rtwdemodir();

2. Open and build the model.

open_system('rtwdemo_async_mdlreftop');
evalc('slbuild(''rtwdemo_async_mdlreftop'')');

3. Create a coder.codedescriptor.CodeDescriptor object for the required model by using the getCodeDescriptor function.

codeDescriptor = coder.getCodeDescriptor('rtwdemo_async_mdlreftop');

4. Get a list of all the referenced models by using the getReferencedModelNames method.

refModels = codeDescriptor.getReferencedModelNames()
refModels =

  1x1 cell array

    {'rtwdemo_async_mdlrefbot'}

5. To obtain the coder.codedescriptor.CodeDescriptor object for the referenced model, use the getReferencedModelCodeDescriptor method.

refCodeDescriptor = codeDescriptor.getReferencedModelCodeDescriptor('rtwdemo_async_mdlrefbot');

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.

Related Topics