Bird
Raised Fist0
Simulinkdata~10 mins

Simulink vs MATLAB scripting - Visual Side-by-Side Comparison

Choose your learning style10 modes available
Concept Flow - Simulink vs MATLAB scripting
Start: Define system goal
↓
Write code commands
↓
Choose other tools
↓
Analyze results & outputs
↓
End process
This flow shows the choice between using Simulink's graphical modeling or MATLAB scripting to build and simulate systems, then analyze results.
Execution Sample
Simulink
% Simulink: Open model and run
open_system('vdp');
sim('vdp');

% MATLAB script: Define and simulate ODE
[t,x] = ode45(@(t,x) [x(2); -x(1)], [0 10], [1;0]);
plot(t,x(:,1));
This code runs a Simulink model named 'vdp' and separately simulates a simple ODE using MATLAB script, then plots the result.
Execution Table
StepActionEnvironmentResult/Output
1Open Simulink model 'vdp'SimulinkModel window opens with block diagram
2Run Simulink simulationSimulinkSimulation runs, outputs generated
3Define ODE function in MATLAB scriptMATLABFunction handle created
4Run ode45 solverMATLABNumerical solution arrays t and x computed
5Plot solution x vs tMATLABGraph window shows solution curve
6End-Simulation and script execution complete
💡 Both Simulink and MATLAB script complete their simulations and produce outputs for analysis.
Variable Tracker
VariableStartAfter Step 3After Step 4Final
tundefinedundefined[0, ..., 10][0, ..., 10]
xundefinedundefined[[1,0], ..., [val,val]][[1,0], ..., [val,val]]
Simulink Modelclosedopenrunningcompleted
Key Moments - 2 Insights
Why do we see a graphical window in Simulink but a plot in MATLAB script?
Simulink uses block diagrams to model visually, so it opens a model window (see Step 1). MATLAB scripting uses code and plots results numerically (see Step 5).
Can we run the same simulation in both Simulink and MATLAB script?
Yes, but Simulink is better for visual system models, while MATLAB scripts are better for custom equations and numerical methods (see Steps 2 and 4).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what happens at Step 2 in Simulink?
AThe model window opens
BODE solver computes solution
CSimulation runs and outputs are generated
DPlot of results is shown
💡 Hint
Check the 'Result/Output' column for Step 2 in the execution table.
According to variable_tracker, when does variable 't' get its numerical values?
AAfter Step 4
BAfter Step 3
CAt Start
DAt Final
💡 Hint
Look at the 't' row values after each step in variable_tracker.
If we only want to write equations and not use block diagrams, which environment do we choose?
ASimulink
BMATLAB scripting
CNeither
DBoth simultaneously
💡 Hint
Refer to concept_flow where MATLAB scripting is chosen for writing code commands.
Concept Snapshot
Simulink vs MATLAB scripting:
- Simulink uses graphical block diagrams for modeling systems.
- MATLAB scripting uses code to define equations and run simulations.
- Simulink is visual and interactive; MATLAB scripting is flexible and code-based.
- Both can simulate dynamic systems and produce outputs.
- Choose Simulink for system design, MATLAB for custom computations.
Full Transcript
This visual execution compares Simulink and MATLAB scripting for system simulation. The flowchart shows choosing between building a block diagram in Simulink or writing code in MATLAB. The sample code opens and runs a Simulink model, then separately solves an ODE with MATLAB script and plots the result. The execution table traces each step: opening the model, running simulation, defining functions, solving numerically, and plotting. Variable tracking shows when variables like time vector 't' and solution 'x' get values. Key moments clarify why Simulink shows a model window and MATLAB shows plots, and when to use each tool. The quiz tests understanding of steps and variable changes. The snapshot summarizes the main differences and use cases for Simulink and MATLAB scripting.

Practice

(1/5)
1. What is the main difference between Simulink and MATLAB scripting?
easy
A. Simulink uses visual blocks; MATLAB scripting uses code.
B. Simulink is only for data analysis; MATLAB scripting is for graphics.
C. Simulink runs faster than MATLAB scripting always.
D. MATLAB scripting cannot create models.

Solution

  1. Step 1: Understand Simulink's approach

    Simulink builds models using visual blocks and diagrams.
  2. Step 2: Understand MATLAB scripting's approach

    MATLAB scripting creates and analyzes models using code commands.
  3. Final Answer:

    Simulink uses visual blocks; MATLAB scripting uses code. -> Option A
  4. Quick Check:

    Visual blocks vs code = D [OK]
Hint: Simulink is visual; MATLAB scripting is text-based code [OK]
Common Mistakes:
  • Thinking Simulink is only for data analysis
  • Believing MATLAB scripting cannot create models
  • Assuming Simulink always runs faster
2. Which of the following is the correct way to open a Simulink model named myModel using MATLAB scripting?
easy
A. open('myModel.slx')
B. sim('myModel')
C. load_system('myModel')
D. run('myModel')

Solution

  1. Step 1: Recall MATLAB commands for Simulink

    The command load_system('modelName') opens the Simulink model without running it.
  2. Step 2: Check other options

    open() is for files but not the recommended for Simulink models; sim() runs the model; run() is for scripts.
  3. Final Answer:

    load_system('myModel') -> Option C
  4. Quick Check:

    Open model = load_system() [OK]
Hint: Use load_system() to open Simulink models in MATLAB [OK]
Common Mistakes:
  • Using sim() to open instead of run the model
  • Using open() which may not open Simulink models properly
  • Confusing run() with model opening
3. Consider this MATLAB script snippet to simulate a Simulink model:
load_system('simpleModel');
simOut = sim('simpleModel', 'StopTime', '10');
result = simOut.get('yout');
disp(result);

What does result contain?
medium
A. The model file path as a string.
B. The simulation output data from the model.
C. The simulation stop time value.
D. An error because get is not a method.

Solution

  1. Step 1: Understand sim() output

    The sim() function returns a Simulink.SimulationOutput object containing simulation results.
  2. Step 2: Use get() method on simOut

    simOut.get('yout') retrieves logged output data named 'yout' from the simulation.
  3. Final Answer:

    The simulation output data from the model. -> Option B
  4. Quick Check:

    simOut.get('yout') = simulation data [OK]
Hint: sim() returns output object; get('yout') fetches data [OK]
Common Mistakes:
  • Thinking get() is invalid method
  • Confusing output data with file path
  • Assuming result holds stop time
4. You run this MATLAB script to simulate a Simulink model:
sim('myModel')

But you get an error: Undefined function or variable 'myModel'. What is the likely cause?
medium
A. You must open the model with load_system() before simulating.
B. The sim() function is not supported for Simulink models.
C. The model name must be a full file path string.
D. The model file 'myModel.slx' is not in the current folder or path.

Solution

  1. Step 1: Understand the error message

    The error means MATLAB cannot find a model named 'myModel' in the current folder or path.
  2. Step 2: Check common causes

    Usually, the model file is missing or not in the MATLAB path, causing this error.
  3. Final Answer:

    The model file 'myModel.slx' is not in the current folder or path. -> Option D
  4. Quick Check:

    File missing = undefined variable error [OK]
Hint: Ensure model file is in current folder or MATLAB path [OK]
Common Mistakes:
  • Thinking sim() cannot run models
  • Believing load_system() is required before sim()
  • Assuming full path is always needed
5. You want to automate running multiple Simulink models and collect their output data using MATLAB scripting. Which approach is best?
hard
A. Write a MATLAB script that loops over model names, uses sim() to run each, and stores outputs.
B. Manually open each model in Simulink and save output files.
C. Use Simulink graphical interface to drag all models into one big model.
D. Run models one by one using the Simulink GUI and copy results manually.

Solution

  1. Step 1: Identify automation needs

    Automating multiple model runs requires scripting to avoid manual work.
  2. Step 2: Use MATLAB scripting capabilities

    Looping over model names and running sim() collects outputs efficiently in code.
  3. Step 3: Evaluate other options

    Manual or GUI methods are slow and error-prone for many models.
  4. Final Answer:

    Write a MATLAB script that loops over model names, uses sim() to run each, and stores outputs. -> Option A
  5. Quick Check:

    Automation with sim() loop = best practice [OK]
Hint: Use MATLAB loops with sim() to automate multiple model runs [OK]
Common Mistakes:
  • Trying to automate using only Simulink GUI
  • Running models manually for many cases
  • Combining models graphically instead of scripting
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard