MATLAB Answers

SimEvents: is it possible to change event action variable from script?

6 views (last 30 days)
Ken Kwon
Ken Kwon on 21 Aug 2019
Answered: Jalaj Gambhir on 27 Aug 2019
Hi. I'm trying to run multiple iterations of SimEvent simulation while sweeping one variable in the model.
Let's say I have an entity with attribute called 'Route', within initial value of 1.
I have this event action in the entity server named 'Entity Server 1'
x = rand;
threshold= 0.5;
if x>threshold
entity.Route = 2;
end
Following the Entity server is 2 port Entity Switch that uses attribute 'Route' to divert entity accordingly.
Is it possible to change variable 'threshold' value from script? I'm trying to plot end output parameter vs threshold plot.
Thank you

  0 Comments

Sign in to comment.

Answers (1)

Jalaj Gambhir
Jalaj Gambhir on 27 Aug 2019
Hi,
I believe variable declared within a block parameter of Simulink block might not be accessible from the MATLAB as it goes out of scope.
However, a possible workaround could be by saving the different threshold value at each iteration in a ‘.mat’ file and then loading the saved value with the ‘Entity action’ tab. You can get an idea from the following script:
values = [0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9];
for i=1:length(values)
threshold = values(i);
save('dump.mat','threshold');
simOut = sim('yourModelName');
end
And in the Entity Action tab:
x = rand;
th = load('dump.mat','threshold');
entity.Route = 1;
if x> th.threshold
entity.Route = 2;
end

  0 Comments

Sign in to comment.

Sign in to answer this question.