Calling a python file from matlab/simulink

1 view (last 30 days)
Dear all,
I have a dataset and I have to apply different machine learning algorithms in it. Because I am more proficient in Python, I considered writing the code in Python.
Now I have a Python file that runs flawlessly and returns the predicted values. However, there is a global simulation environment in which these values must be integrated, and this global simulation environment is based on Matlab/Simulink.
I'm thinking about modeling 1D look-up tables in Simulink using the output dataset from the executed Python file. My question is, how do I get Simulink to call the Python file?
Example: A user runs the global environment model in Matlab/Simulink that calls the python file, the python files executes and saves the output in .csv format, and the look-up tables are updated and accessed in the workspace of matlab that are eventually run in Simulink.

Answers (1)

David Willingham
David Willingham on 14 Sep 2022
Hi Rehan,
What application is the python model being used for? In simulink, do you need the model to run every time step or do you only need to run prior to the start or end of a simulation? I see you gave some use cases but it's not 100% clear.
  2 Comments
Rehan Khan
Rehan Khan on 15 Sep 2022
Hi David,
Thank you for taking out some time for my question.
I can give you a short summary of my global environment and what role python plays in it. The global environment is an energy management system (EMS) for a hybrid system of lithium-ion battery and a solid oxide fuel cell (SOFC). According to the load required, the EMS distributes the power among the two different propolsion technologies. It does it by following some rules applied in the EMS. Now, both the models of lithium-ion battery and SOFC are to be integrated into the EMS. The battery model already exists in Simulink and was a part of a thesis of a different study.
My work is to develop a digital twin model of SOFC. I have experimental data of diffferent parameters but only for limited operation conditions. I wil apply different machine learning algorithms to check which algorithm suits our application best. Now once the algorithm runs without an error it will give me output in the form of parameter values but in different operating conditions that have not yet been measured before.
Having a better hand at python, I have developed my models in python. I get .xlsx or .csv files as output. But since python and Simulink are two diferent simulation tools, someone who does not have knowledge of running python can have difficulty in running my model to get the output.
Question: My question is how can I call these python files that contain my model to run from the simulink environment. Example: User runs the simulink model, it triggers the python file and executes it that gives output in the form of excel or csv, now as soon as these excel files are updated, the matlab automatically uploads them in the workspace, the 1D look-up tables that are already defined in Simulink gets connected to these excel files as the the parameter name that are defined in the 1D look-up table model in Simulink are already present in the excel files as parameter names. The model does not need to run every time step, only once.
I am looking forward to hearing from you.
Best regards,
Rehan
David Willingham
David Willingham on 15 Sep 2022
Hi Rehan,
Thanks for providing the additional context to the problem you're working on. It is very helpful to hear.
There is a post that describes how to call Python via the MATLAB function block in Simulink:
Try using this method, if you have questions please don't hesitate to reply here.
David

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!