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.

open_system('ex_nmea_gps_data_parser_simulink_pyClass');

Use Python Importer to Integrate Python Functions into Simulink

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

obj = Simulink.PythonImporter;
view(obj);

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

Load the bus objects used for this example.

load('pyNMEABuses.mat'); 

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.

PythonImporter_library.png

3. On the Specify Custom Code page, in the Files to Import box, specify pyFile.py 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.

PythonImporter_file.png

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

PythonImporter_selection.png

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.

PythonImporter_configPort.png

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.

open_system("PythonLibrary_DecodersClassifier.slx"); 

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.

open_system("ex_nmea_gps_data_parser_python_pyClass.slx"); 

See Also

Related Topics