How to manage multiple matlab processes running at the same time from python

10 次查看(过去 30 天)
Hello,
Short Overview
I have a python program, where you can start a matlab script from.
This script collects parameters e.g. initializes them by accessing a database and starts a simulink simulation.
After the simulation is done something is plotted by another matlab function.
For some reason the plot functions hangs when I do simulation + potting in one matlab call.
I couldn't figure out why.
It works fine, when I call them seperately e.g. quit the engine from python, start a new engine and call the plot function. That raises the need to know when my simulation is done, so I can then call the matlab plot function.
Files
I added a text file where you can see my Simulation class in python. It initializes paths for matlab and python modules.
When a simulation is executed by the method execute_simulation in the ThermalSimulation class (see text file), then the matlab process detaches. I can't run the plotting function because the data is not yet generated.
I added the script where parameteres are initialized for the simulink script: "run_script.m"
And the plotting function for the plotting afterwards: "Temperature_Plot_Simulation.m"
Question
The question now is how can I obtain information about when a matlab function is finishe?
In "run_script.m" both, "save_parameters.m" and "Temperature_Plot_Simulation.m" are called, but as I said they DO NOT work, if and only if I call them from my python class from the matlab engine. But they DO from matlab.
I can get the matlab engine process id, e.g. with feature('get_pid') but this pid is from the engine. And I need to know when the simulink simulation is actually finished. Is there a way to get this information? Because only then I can quit the engine and start a new one.
I appreciate any help.
If you happen to know why the plotting won't work in the first place I am happy to know ( the function hangs, when "hold on; grid on; grid minor;" is executed successfully, and then no more ), but I didn't want to ask about both subjects in this question.
Unfortunately you can't run the code really. For the ThermalSimulation you lack the data object, and the simulink model of course.
if any more information is needed, let me know about it.

回答(1 个)

Swastik
Swastik 2024-5-22
It sounds like you are encountering challenges with plotting simulation results from Simulink and have identified a workaround that involves restarting the Python engine between the simulation and the MATLAB plotting script. To address the issue of the Python process detaching after running the simulation script, I have a couple of suggestions that could streamline your workflow.
Ensuring smooth communication between the simulation completion in Simulink and subsequent MATLAB processes for plotting is your main concern. To facilitate this communication, consider utilizing a shared file as a signalling mechanism. This approach involves writing to a text file at the end of your simulation to signal completion, which can then be checked by other MATLAB processes.
At the end of your Simulink simulation, use the writelines function to write a specific line or flag to a text file indicating that the simulation has finished. More details on this can be found here:
In your MATLAB script responsible for plotting, use the fileread function to check this file for the completion flag before proceeding with plotting. Further information on this function is available here:
Instead of using the sim function, which halts the MATLAB script until the simulation is complete, you can use set_param with your loaded system to start the simulation in a non-blocking manner:
set_param(gcs, SimulinkCommand=”start”)
To check the status of the simulation, utilize:
get_param(gcs, “SimulationStatus”)
This method allows your MATLAB script to continue executing while the simulation runs. You can learn more about managing simulations programmatically here:
Hope this helps.

产品


版本

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by