Main Content

Integrate Python Functions Specified Within Classes Using Python Importer

This example shows how to use Python® Importer to import Python functions specified within modules that contain class and function definitions that parse NMEA GPS messages into Simulink®.

In the baseline model ex_nmea_gps_data_parser_simulink_pyClass.slx, built-in Simulink blocks are used to classify and decode RMB and RMC sentences. For more information about the model and using Simulink blocks, see Parse NMEA GPS Text Message. This example uses Python Importer to generate custom MATLAB System blocks from a Python file that implements RMB and RMC classification and decoding. The generated custom blocks replace the built-in Simulink blocks in the baseline model.

To call Python modules in MATLAB, you must have a compatible version of Python installed. For more information on supported Python versions and setting up your system to use Python in MATLAB, see Configure Your System to Use Python.

Explore Baseline Model

In the baseline model, the subsystem named NMEA Data Generator generates data and sends the data to the subsystem named Data Classifier. The Data Classifier subsystem classifies the sentences as RMB, RMC, or unsupported. The sentences are then sent to RMB Decoder, RMC decoder, or Unsupported, depending on their type. The RMB Decoder and RMC Decoder subsystems contain RMBDecoder and RMCDecoder blocks that are built-in Simulink blocks implementing RMB and RMC decoding, respectively.


Use Python Importer to Integrate Python Functions into Simulink

1. Open Python Importer as a standalone application and click Start.

obj = Simulink.PythonImporter;

For more information on Python Importer, see Import Python Code to Simulink Using Python Importer Wizard.

Load the bus objects used for this example.


2. On the Settings page, in Simulink library file name box, specify the library file name. In the Output folder box, specify the path of folder where you want the Simulink library and artifacts to be located. Click Next.


3. On the Specify Custom Code page, in the Files to Import box, specify as the source file. pyFile is a Python module that contains a class decoder that defines two methods that implement RMB and RMC decoding. The module also contains the function classify that implements classification of the messages.

Click Next.


4. On the Import page, select the pyFile.classify, pyFile.decoder.RMB, and pyFile.decoder.RMC functions for import. Click Next.


5. Specify the data type of input as string for all the functions. For the pyFile.decoder.RMB function, specify data type of the output as Bus: NMEA_GPRMB. For the pyFile.decoder.RMC function, specify data type of the output as Bus: NMEA_GPRMC. Set string as the output data type for both the outputs of the pyFile.classify function.

The class constructor arguments in your class definition file appear as block parameters of the generated block. You can configure the label, type, complexity and size of the parameters at this step if they are defined in the source file.

Click Next.


6. On the Create Simulink Library page, check the specification of the library to be generated. Click Next to generate the library.

7. The Python Importer generates a custom Simulink library containing MATLAB System blocks for the selected functions. Click Finish.

Open the library containing the generated blocks.


8. In the baseline model, replace the RMBDecoder and RMCDecoder blocks with the generated pyFile_decoder_RMB and pyFile_decoder_RMC blocks, respectively. Replace the Classifier subsystem with the generated pyFile_classify block.

Open this model to explore the implementation of the generated blocks in place of the built-in Simulink blocks for decoding GPS messages.


See Also

Related Topics