user10678
user10678

Reputation: 159

How to declare global variables for simulink containing matlab functions?

I'm building a Simulink model with multiple Matlab function blocks. Each of these function blocks has a lot of constants, for example, g = 9.8, common between them. I want to initialize all of these constants in one go so that I don't have to do so in each function block.

I tried initializing all the variables in Matlab workspace, but they don't seem to be working.

What is the best and easiest way to go forward with this?

Upvotes: 5

Views: 21748

Answers (2)

Foad S. Farimani
Foad S. Farimani

Reputation: 14008

The canonical method for adding variables/constants across your Simulink model is to use the Model Explorer from the MODELING tab or by pressing Ctrl+H:

enter image description here

select either the Base Workspace or the ModelWorkspace

enter image description here

from the Meubar select Add and then select the MATLAB Variable or press Ctrl+M.

enter image description here

change the variable name by double-clicking it in the table and its value on the right panel:

enter image description here

Upvotes: 1

Mohsen Nosratinia
Mohsen Nosratinia

Reputation: 9864

You need to either define your global variables as Simulink.Signal object or a Data Store Memory block and then define them as global in your MATLAB function. You cannot access the regular MATLAB variables from MATLAB workspace.

There is a detailed page about this on MAthworks website with a good example.

Upvotes: 5

Related Questions