Python can't send value to simulink constant block

10 views (last 30 days)
This is my simulink. I want to send the value from my python to constant block of u1 and u2, then the x1 and x2 will send the output to my python. Please ignore u2, cuz still in testing phase.
this is part of my python code, the set control action is used to send the value to u1 and u2 in simulink. This is the issue where the simulink cant access to the value sent from python. The output dont have issue.
Here is the output, as shown above the output x1 is showing 1.0 which shows that the constant block is not deliver the input values from python.
SO is there anyway to solve this issue? anything i need to check or set in the simulink? or any website or code I can refer to? any help or recommendation will be appreaciate. Thank you very much.

Accepted Answer

Ayush Singh
Ayush Singh on 8 Jul 2024
Edited: Ayush Singh on 8 Jul 2024
Hi Yeow,
I am assuming you want to assign values to constant blocks u1 and u2 from Python and extract values of x1 and x2 after some simulation on your model.
In order to achieve the workflow mentioned by you there are roughly 3 steps can follow:
  1. Set values to the constant blocks u1 and u2 from Python.
  2. Run the Simulink model.
  3. Retrieve the outputs x1 and x2 from the MATLAB workspace back to Python.
Start the MATLAB engine
eng = matlab.engine.start_matlab()
Load the Simulink model
eng.load_system(model_name, nargout=0)
Set values to the constant blocks u1 and u2
eng.set_param(f'{model_name}/u1', 'Value', str(u1), nargout=0)
eng.set_param(f'{model_name}/u2', 'Value', str(u2), nargout=0)
Run the Simulink model
eng.set_param(model_name,SimulationCommand="start")
Wait for the simulation to finish
eng.set_param(model_name,SimulationCommand="stop")
Retrieve the outputs x1 and x2
x1 = eng.eval('out.x1', nargout=1)
x1_data =x1.Data
x2 = eng.eval('out.x2', nargout=1)
x2_data =x2.Data
The above steps/code might not be exactly according to your coding structure but is a rough idea/implementation on how you can achieve the desired workflow.
I hope it helps you to get started!

More Answers (1)

Yeow Yu Leong
Yeow Yu Leong on 8 Jul 2024
Hi @Ayush Singh, Thanks for your reply.
Currently, I am using another structure to extract and send the data. Is working for me. Here is my code:
import matlab.engine
import numpy as np
import time
###this one is working 5/7/24
# Start MATLAB engine
eng = matlab.engine.start_matlab()
# Load the Simulink model
eng.load_system('tracking3')
# Main loop to send random input and retrieve output
num_cycles = 500 # Number of simulation cycles
for cycle in range(num_cycles):
# Generate a random value for the constant block
u1_value = np.random.random()
# Set the value for the constant block 'Constant'
constant_block_path = 'tracking3/Constant'
eng.set_param(constant_block_path, 'Value', str(u1_value), nargout=0)
# Run the Simulink model
eng.set_param('tracking3', 'SimulationCommand', 'start', nargout=0)
status = eng.get_param('tracking3', 'SimulationStatus')
while status != 'stopped':
status = eng.get_param('tracking3', 'SimulationStatus')
# Retrieve the output value from 'x1' in 'out'
x1_value = eng.eval('out.x1')
first_x1_value = x1_value[cycle][0]
print(f'Cycle {cycle+1}: Input = {u1_value:.4f}, Output = {first_x1_value:.4f}')
# Pause for a moment before next cycle (optional)
time.sleep(0.1) # Adjust as needed
# Close the Simulink model without saving
eng.close_system('tracking3', 0, nargout=0)
# Stop MATLAB engine
eng.quit()

Products


Release

R2023b

Community Treasure Hunt

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

Start Hunting!