How to use a variable as parameter of a s-function in simulink

1.9k views Asked by At


I want specify some parameters of a s-functions using a variable and not using a constant inserted manually.
I have tried to define global variables in matlab and using them as paramaters and it works fine.
Anyway, i want that a variable computed in simulink is used as parameter of an other s-fuction. Is it possible? If so, how can i do it?
One idea was to continue to use global constants in matlab e to update them from simulink but i'm not able to do it because the "to workspace" block transforms them in struct or array and i need a single value.

Edit: To update the variable in the global variable in the workspace is not useful because it seems that simulink read the values at the beginning

2

There are 2 answers

3
am304 On

The only solution I can think of would be to have the "variable" as one of the input signals to the S-function block, with the signal coming from some other part of the Simulink model where it's computed.

0
Antonio On

I have found a solution. First of all, it's important to know that:

  1. a s-function paramenter can be: a constant, a workspace variable, a matlab expression. In matlab expression are included calls to matlab functions.
  2. simulink computes the value of each parameter at the onset of the simulation (it has sense!).

The solution is: use a matlab function for computing the parameter. a matlab function can call an other simulink simulation. I tried it, even if in a very simple case, and it worked.

I know, it's a crazy solution but it is a solution. Warning: at the moment i'm not able to know if this crazy solution has some side effects on the simulation.

The best solution to this problem is to convert the paramenter in input of the s-function but i can't do it because it's a 3rd party sfunction