After a while I managed to find a solution that solved my problem!
By adding one more input parameter to the matlab function and feeding that input with a constant value-block:
function [outputPort,log] = failureRate(time,lambda,nSensors,seed)
persistent failed failedCount
if isempty(failed)
failed = false(1,nSensors);
failedCount = 0;
rng(seed) % Initiate with seed from constant block outside of function.
end
% Failure rate function and random variable
failRate = @(t,lam) 1 - exp(-t*lam);
r = rand(1,nSensors);
nBroken = 0;
if any(r < failRate(time,lambda))
tmp = failed;
failed = max(failed, r < failRate(time,lambda));
log = [0,0];
if failedCount < sum(failed)
failedCount = sum(failed);
nBroken = sum(failed - tmp);
end
end
if all(failed)
% All sensors broken
outputPort = 2;
log = [nBroken,time];
else
% Still working
outputPort = 1;
log = [nBroken > 0, time];
end
end
Then for each simulation in the SimulationInput-object randomize the seed value as below:
seedBlockPath = 'SensorFailure/Subsystem/Simulink Function/Seed';
for i = 1:n
% Set random seed for each simulation
simIn(i) = Simulink.SimulationInput('SensorFailure');
r = randi(99999);
simIn(i) = simIn(i).setBlockParameter(seedBlockPath,'Value',num2str(r));
end
out = parsim(simIn,...
'ShowSimulationManager','off',...
'ShowProgress','on',...
'TransferBaseWorkspaceVariables','on',...
'UseFastRestart','on',...
'StopOnError','on');