function [Li_current] = light_influent [SimulationTime]
switch flag_light
case 0
%Light intensity is set to a constant value for the whole simulation
Li_current= pKt.Li
case 1
%Light intensity periodically alternates between 'on' at a set value or 'off'
get_param (MyModel, SimulationTime)
答案 0 :(得分:0)
这里的技巧是使用持久变量。基本上,您需要跟踪上次打开灯的时间,以及灯是打开还是关闭。这是一个最小的工作示例,它将每秒打开和关闭灯
function [Li_current] = light_influent(SimulationTime)
% Declare and initialize the persistent variable during the first run
persistent lastTime
persistent lightOn
if isempty(lastTime)
lastTime = SimulationTime;
end
if isempty(lightOn)
% Start with the light off
lightOn = false;
end
disp([ lastTime, SimulationTime, lightOn ] )
% Declared here for testing purposes...
flag_light = 1;
switch flag_light
case 0
%Light intensity is set to a constant value for the whole simulation
Li_current = 1;
case 1
%Light intensity periodically alternates between 'on' at a set value or 'off'
% Switch every one second.
switchFrequency = 1;
if (SimulationTime - lastTime) > switchFrequency
% If the light is on, turn it off. If off, turn it on
lightOn = ~lightOn;
lastTime = SimulationTime;
disp('switched')
end
if lightOn
Li_current = 1;
else
Li_current = 0;
end
end