Azzera filtri
Azzera filtri

Matlab Function in simulink

1 visualizzazione (ultimi 30 giorni)
LODOVICO MANILDO
LODOVICO MANILDO il 22 Giu 2021
Commentato: Walter Roberson il 22 Giu 2021
Hi everyone,
I would like to create a Matlab Function in a simulink model. The function would be used in order to create a mask in which I can choose one over 4 different cases. For each one of this cases I would like to have a dedicated simulation stop time.
My code is this one:
function y = fcn(value)
T_sim=0;
if (value==1)
T_sim = inf;
elseif (value==2)
T_sim = 10;
elseif (value==3)
T_sim = 15;
else
T_sim = 20;
end
y = value;
I'm trying with this code, but I think that T_sim variable is local, and then outside from that function it has no effect. How can I solve it?

Risposte (1)

Walter Roberson
Walter Roberson il 22 Giu 2021
If you look at the first diagram there, imagine using the output from your function block instead of a Constant block. You would compare to the Clock, and when the limit was reached, the STOP block would act.
  2 Commenti
LODOVICO MANILDO
LODOVICO MANILDO il 22 Giu 2021
Thanks for answering that fast, however its not usefull in this case, since the output of the function is the value itself.
I'm wondering to understand how can I update, let's say "automatically", the dedicated stop time variable, which in this case is called "T_sim".
I even tried to declare it as:
global T_sim;
But it doesn't work.
As you can see from the attached picture, the value is used to select one of the four possible speed references which have different durations.
Walter Roberson
Walter Roberson il 22 Giu 2021
Well, don't do that then: make T_sim a second output from the MATLAB Function Block.
If you are for some reason required to make T_sim a global variable in order to access it from other places, then you have to register it as a datastore https://www.mathworks.com/help/simulink/ug/model-global-data-using-data-stores.html
... but it seems to me much easier to just emit it from the function block as an additional output.

Accedi per commentare.

Categorie

Scopri di più su Event Functions in Help Center e File Exchange

Prodotti

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by