Filter löschen
Filter löschen

Matlab Function in simulink

1 Ansicht (letzte 30 Tage)
LODOVICO MANILDO
LODOVICO MANILDO am 22 Jun. 2021
Kommentiert: Walter Roberson am 22 Jun. 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?

Antworten (1)

Walter Roberson
Walter Roberson am 22 Jun. 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 Kommentare
LODOVICO MANILDO
LODOVICO MANILDO am 22 Jun. 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 am 22 Jun. 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.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Event Functions finden Sie in Help Center und File Exchange

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by