sim command in try/catch, in parfor results in error
조회 수: 14 (최근 30일)
이전 댓글 표시
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
댓글 수: 0
채택된 답변
Edric Ellis
2018년 8월 17일
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
댓글 수: 0
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!