KhaledWas
KhaledWas

Reputation: 73

Control Variables in SIMULINk

I'm new to Matlab and SIMULINK, and I know that this might be easy. But I just can't find the answer on the internet.

I'm building a SIMULINK model (group of blocks) and I want to set the values inside the blocks as variables so I can maybe control it from an m file or something. How can I do this?

Upvotes: 0

Views: 2182

Answers (2)

Akanksha
Akanksha

Reputation: 96

In the simulink model, In the 'Value' field of a Constant Block, enter the variable name. The constant block will then look like this: (see uplim and lowlim)

enter image description here

Now, whenever you wish to change the variable's value, execute the following commands through an m-file:

Let's assume the variable's name is pressure and the new value is 5.

assignin('base','pressure',5); set_param('path of constant block','Value','pressure');

The path to a constant block (or any simulink block) looks something like this: modelname/Constant2 (considering it is the top level of your model; Constant Block number can vary)

Upvotes: 0

kabdulla
kabdulla

Reputation: 5419

As @rayryeng has pointed out you can just type the name of a variable in place of the parameter value of blocks, and then whatever value that variable is set to in your Matlab workspace will be used.

Whenever I do this, I like to set default values of the variables in the models intialization callback function details here. That way your model is portable and will run on it's own.

Upvotes: 1

Related Questions