sim command in try/catch, in parfor results in error

18 Ansichten (letzte 30 Tage)
Ryan
Ryan am 15 Aug. 2018
Beantwortet: Edric Ellis am 17 Aug. 2018
I'm using parfor to execute Monte Carlo runs of a Simulink diagram. It was working fine, but I added a try/catch statement to avoid aborting the loop if one of the Monte Carlo draws results in an error.
clear simout
parfor ii=1:2
try
simout(ii) = sim('sandbox');
catch ME
ME
disp(ME.message)
end
end
This code results in the following error every time (Sandbox is just a dummy diagram but it still results in an error):
MException with properties:
identifier: 'MATLAB:UnableToConvert'
message: 'The following error occurred converting from Simulink.Sim...'
cause: {}
stack: [2×1 struct]
The full error message is
The following error occurred converting from Simulink.SimulationOutput to double:
Conversion to double from Simulink.SimulationOutput is not possible.
If the parfor is changed to for or if the try/catch is removed from the parfor, the code runs without error. It's only when the try/catch is used with the parfor that I get this error. It could have something to do with the Single Simulation Output, but I believe using this data structure is necessary when using sim in a parfor. Any ideas on the root cause of the error and potential workarounds?
Thanks, Ryan

Akzeptierte Antwort

Edric Ellis
Edric Ellis am 17 Aug. 2018
I'm not 100% sure what's going on there, but it might work simply to say:
simout = cell(1,2);
parfor ii=1:2
try
% note use of cell array indexing:
simout{ii} = sim('sandbox');
catch ME
ME
disp(ME.message)
end
end

Weitere Antworten (0)

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange

Produkte


Version

R2016b

Community Treasure Hunt

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

Start Hunting!

Translated by