How to Pass Input from Python Script to Simulink Model
12 views (last 30 days)
Show older comments
I had one DC-DC converter simulink model available with me and I am using MATLAB WITH PYTON to run that mode;. I had write down the code but i am facing an issue like my input to simulink model in form of (t,u) is not passing correctly and because of this error was comming. Please help me to fix this issue.
Detail code is as follow:
import matlab.engine
import numpy as np
import matplotlib.pyplot as plt
# Start MATLAB engine
eng = matlab.engine.start_matlab()
# Load the Simulink model
eng.load_system('VMC_BOOST3')
# Define the fixed duty cycle value
duty_cycle = 0.4 # 40% duty cycle
# Define time vector
t = np.linspace(0, 0.05, 100)
# Define input signal (single variable for model)
u = np.full_like(t, duty_cycle) # Constant input signal at duty cycle
# Convert to MATLAB format
time_matlab = matlab.double(t.tolist())
input_matlab = matlab.double(u.tolist())
# Combine time and input into a single matrix
external_input = matlab.double(np.column_stack((t, u)).tolist())
# Pass these to MATLAB workspace
eng.workspace['external_input'] = external_input
# Run the Simulink model and capture outputs
print("Running Simulink model...")
sim_out = eng.sim('VMC_BOOST3', 'ExternalInput', 'external_input', nargout=1) # Replace with your Simulink model name
# Retrieve the output voltage (Vout) and time data
yout = eng.getfield(sim_out, 'yout') # Access the yout variable
time = eng.getfield(sim_out, 'tout') # Retrieve time data
# Convert yout and time to NumPy arrays
yout = np.array(yout) # Convert MATLAB array to NumPy array
time = np.array(time)
# Extract Vout (second column of yout)
Vout = yout[:, 1] # Second column corresponds to Vout
# Compute the average value of Vout
average_Vout = np.mean(Vout)
print("Average output voltage (Vout):", average_Vout)
# Plot the output voltage over time
plt.figure(figsize=(10, 6))
plt.plot(time, Vout, label='Output Voltage (Vout)', color='blue')
plt.axhline(y=average_Vout, color='red', linestyle='--', label=f'Average Vout: {average_Vout:.2f} V')
plt.xlabel('Time (s)')
plt.ylabel('Output Voltage (V)')
plt.title('Boost Converter Output Voltage')
plt.legend()
plt.grid(True)
plt.show()
# Stop MATLAB engine
eng.quit()
1 Comment
Hitesh
on 4 Mar 2025
Could you include the error message along with the 'VMC_BOOST3' sample model? This will help in reproducing and addressing this issue.
Answers (1)
Simon
on 13 Aug 2025
Hi Ashish,
As I understand, you’re running a Simulink DC-DC converter model from Python. Your code seems fine, but the Simulink model must be configured correctly. Without the exact error or model details, I’ve tried this with a sample DC converter model. Please ensure your model setup matches the code’s requirements:
- Add an Inport block at the top level of your Simulink model.
- Connect the Inport to the part of the circuit where you want the duty cycle input to be applied.
- Make sure the Inport block is configured for a scalar signal (since u is a scalar).
- The external input should be a 2-column matrix.
- The simulation stop time must match the last value in your time vector t.
- If the To Workspace block is not set up to log to Simulation Output, you won’t get your results back in Python.
Any of these issues could cause the error. If not, please provide more details.
Please find the attached image to cross-check your model, and refer to the following documentation for more information:

Hope this helps!
0 Comments
See Also
Categories
Find more on Call MATLAB from Python in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!