Skip to content
Tutorials Center
My learning

Topics

Lesson 7 of 36 ยท Blocks and Models

Simulink Block Parameters, Workspace Variables and Data Dictionaries

Learn how Simulink block parameters use MATLAB expressions, where variables live (base workspace, model workspace, data dictionary), parameter objects and model callbacks.

Typing 2.5 into a Gain block works for a demo, but a real model has dozens of values that repeat: a mass, a gain, a sample time. If each lives in a different dialog, changing a design means hunting through the diagram. The solution is to give parameters names and keep them in one place. This lesson shows where Simulink looks for those names, how to keep them organized, and how to make them tunable.

Simulink is not available on this site. The MATLAB text below is for the real product; a Python stand-in imitates the name lookup so you can see its logic.

Parameters are expressions

In a block dialog, a parameter field accepts any MATLAB expression that evaluates to the right kind of value:

TEXT
Gain:            Kp
Constant value:  [1 0 0]
Frequency:       2*pi*50
Sample time:     Ts
Upper limit:     umax * 0.9

Simulink evaluates the expression when the model is compiled (when you run it or press Ctrl+D to update the diagram). If the name is not defined you get an error such as "Undefined function or variable 'Kp'", and the offending block is highlighted.

Where variables live

Place Lifetime Good for
Base workspace Until you clear it or close MATLAB Quick experiments
Model workspace Saved inside the model file Parameters private to one model
Data dictionary (.sldd) A separate file shared by models Team projects, many models
Mask workspace Inside one masked block Library blocks (see Masks, Libraries and Model Reference)

When Simulink resolves a name, it searches from the inside out: mask workspace, then model workspace, then the data dictionary linked to the model, then the base workspace. The first match wins.

Where a name is found

Mask workspace Innermost
Model workspace Inside the .slx
Data dictionary Shared .sldd file
Base workspace Outermost

First match wins.

1 / 2

Setting up parameters

The simplest way is a script that runs before the model, often named init_myModel.m:

MATLAB
% init_cruise.m
m   = 1200;      % vehicle mass, kg
Cd  = 0.32;      % drag coefficient
Kp  = 40;        % proportional gain
Ts  = 0.01;      % controller sample time, s

To run it automatically each time the model loads, go to Modeling > Model Settings > Model Properties > Callbacks and add the script name to PreLoadFcn (runs before the model loads) or InitFcn (runs at the start of each simulation). The other callbacks are PostLoadFcn, StartFcn, StopFcn and CloseFcn.

Model workspace and Model Explorer

Open Modeling > Model Explorer (or Ctrl+H) and select Model Workspace. You can add variables, choose the Data source (Model File, MATLAB Code or MAT-File) and edit values. A MATLAB Code source recalculates variables every time, so derived values stay consistent:

MATLAB
Ts = 0.01;
N  = round(10 / Ts);

Parameter objects

MATLAB variables like Kp = 40 are plain numbers. A Simulink.Parameter object adds metadata that matters for code generation and tuning.

MATLAB
Kp = Simulink.Parameter(40);
Kp.DataType     = 'single';
Kp.Min          = 0;
Kp.Max          = 100;
Kp.Unit         = 'N/(m/s)';
Kp.CoderInfo.StorageClass = 'ExportedGlobal';  % a named global in generated C code

In generated code (see the Code Generation chapter) a plain number is baked into the code as a constant. A parameter object with a storage class that is not Auto stays a tunable variable in memory, so calibration tools can change it without recompiling. Min and Max are checked during simulation.

Tunable and non-tunable parameters

Some parameters can change while the simulation runs: edit a Gain value in the dialog, click Apply, and the next step uses the new value. Others, such as the sample time or the size of a vector, are structural: changing them needs a restart. Dialogs mark such fields as non-tunable when the model is running (they become gray).

Data dictionaries

A data dictionary stores variables in a file with extension .sldd. To link one: Modeling > Model Settings > Model Properties > Data Dictionary, or from MATLAB:

MATLAB
d = Simulink.data.dictionary.create('cruise.sldd');
set_param('cruise_model', 'DataDictionary', 'cruise.sldd');
sec = getSection(d, 'Design Data');
addEntry(sec, 'Kp', 40);
saveChanges(d);

Dictionaries keep data out of the base workspace, avoid collisions between models and can reference other dictionaries.

Practice with a stand-in

Python Stand-in: resolving names like Simulink (Python)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Python Stand-in: parameter limits like Min and Max (Python)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Good habits

  • Never hide magic numbers inside blocks; name them.
  • Keep one init script or one data dictionary per model so it runs on any machine.
  • Give parameters units and limits.
  • Avoid depending on the base workspace in shared models, since someone else's workspace may not contain your variables.

A variable defined in the base workspace can shadow or be shadowed by a variable with the same name elsewhere. If a model behaves differently on two machines, check which workspace supplied the value.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. A model uses Kp in a Gain block. Where does Simulink look first?

    Names resolve from the innermost scope outward.
  2. 2. Which callback runs at the start of each simulation?

    InitFcn runs at the start of simulation initialization; PreLoadFcn runs before the model loads.
  3. 3. What does a Simulink.Parameter with a non-Auto storage class provide?

    It keeps the value as a named variable in memory instead of a hard-coded constant.
  4. 4. Where are .sldd files used?

    A data dictionary is a file that holds model data shared by one or more models.

Summary

  • Block parameters are MATLAB expressions evaluated at compile time.
  • Names resolve from mask to model workspace to data dictionary to base workspace.
  • Use init scripts, model callbacks or data dictionaries to define parameters reproducibly.
  • Simulink.Parameter adds type, limits, units and storage class; some parameters are tunable during a run.

Next: Execution Order and Algebraic Loops