📖 Scenario: You are working on a simple control system project. You want to understand how to interact with Simulink models using MATLAB code. This will help you automate tasks like opening models, running simulations, and extracting results.
🎯 Goal: Build a small MATLAB script that opens a Simulink model, sets a simulation stop time, runs the simulation, and retrieves the simulation output.
📋 What You'll Learn
Create a variable with the Simulink model name
Set the simulation stop time using a variable
Use the sim function to run the model simulation
Extract the simulation output from the simulation result
Display the simulation output
💡 Why This Matters
🌍 Real World
Automating Simulink simulations saves time and reduces manual errors when testing control systems or signal processing models.
💼 Career
Many engineering and data science jobs require running and analyzing simulations efficiently using MATLAB and Simulink interfaces.
Progress0 / 4 steps
1
Create a variable for the Simulink model name
Create a variable called modelName and set it to the string 'simple_model' which is the name of the Simulink model you want to work with.
MATLAB
Hint
Use single quotes to define the string in MATLAB.
2
Set the simulation stop time
Create a variable called stopTime and set it to the string '10' to specify the simulation stop time in seconds.
MATLAB
Hint
The stop time must be a string when used in Simulink simulation settings.
3
Run the simulation using the sim function
Use the sim function with the variables modelName and stopTime to run the simulation. Store the result in a variable called simOut. Use the syntax: simOut = sim(modelName, 'StopTime', stopTime);
MATLAB
Hint
The sim function runs the model and returns simulation data.
4
Extract and display the simulation output
Extract the simulation output data from simOut by accessing the yout property. Store it in a variable called simData. Then, display simData using disp(simData).
MATLAB
Hint
The yout property contains the simulation output signals.
Practice
(1/5)
1. What is the primary purpose of the Simulink interface in MATLAB?
easy
A. To write low-level assembly code
B. To build system models visually using blocks
C. To create 3D animations
D. To perform statistical data analysis
Solution
Step 1: Understand Simulink's role
Simulink is designed for visual modeling of systems using blocks.
Step 2: Compare options
Options B, C, and D describe unrelated tasks to Simulink's main function.
Final Answer:
To build system models visually using blocks -> Option B
Quick Check:
Simulink = Visual block modeling [OK]
Hint: Simulink is about visual block diagrams, not coding [OK]
Common Mistakes:
Confusing Simulink with MATLAB scripting
Thinking Simulink is for data analysis
Assuming Simulink creates animations
2. Which MATLAB command is used to open a Simulink model named myModel?
easy
A. open('myModel.slx')
B. sim('myModel')
C. load('myModel')
D. run('myModel')
Solution
Step 1: Identify command to open model
The open command with the model filename opens the Simulink model.
Step 2: Differentiate other commands
sim runs the model, load loads variables, run executes scripts.
Final Answer:
open('myModel.slx') -> Option A
Quick Check:
Open model = open('filename') [OK]
Hint: Use open('model.slx') to open Simulink models [OK]
Common Mistakes:
Using sim() to open instead of run the model
Confusing load() with open()
Trying run() which is for scripts
3. What will be the output of the following MATLAB commands?
Assuming vdp is a valid Simulink model and simulation runs successfully.
medium
A. The total simulation time
B. An error because open_system does not run the model
C. The last data value from the first logged signal of the simulation
D. The initial value of the simulation signal
Solution
Step 1: Understand commands
open_system('vdp') opens the model, sim('vdp') runs it, and logsout.getElement(1).Values.Data(end) accesses the last data point of the first logged signal.
Step 2: Interpret output
The command returns the final value of the first signal logged during simulation.
Final Answer:
The last data value from the first logged signal of the simulation -> Option C
Hint: Ensure model file is in current folder or path [OK]
Common Mistakes:
Assuming open_system must be called before sim
Thinking sim only works on built-in models
Believing model names must be uppercase
5. You want to programmatically change the value of a block parameter named Gain in a Simulink model controlSys before running the simulation. Which sequence of commands correctly does this?
hard
A. load_system('controlSys'); set_param('controlSys', 'Gain', '5'); sim('controlSys');
B. open_system('controlSys'); set_param('Gain', 'Gain', 5); sim('controlSys');
C. sim('controlSys', 'Gain', 5);
D. set_param('controlSys/Gain', 'Gain', '5'); sim('controlSys');
Solution
Step 1: Identify correct set_param usage
The set_param function requires the full block path and parameter name as strings. 'controlSys/Gain' is the block path, 'Gain' is the parameter, and '5' is the new value as a string.
Step 2: Confirm simulation command
After setting the parameter, sim('controlSys') runs the model with updated parameters.
Final Answer:
set_param('controlSys/Gain', 'Gain', '5'); sim('controlSys'); -> Option D
Quick Check:
set_param with full path + sim() runs updated model [OK]
Hint: Use set_param('model/block', 'param', 'value') before sim() [OK]
Common Mistakes:
Using set_param without full block path
Passing numeric value instead of string for parameter
Trying to pass parameters directly in sim()
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.