programmatically collect sim errors
16 views (last 30 days)
Show older comments
I would like to collect up the errors and warnings related to a model simulation. Help docs have run me down the sldiagviewer.diary rabbit hole to record such to a diary file. (Looking for a better solution here as this is just plain klunky). The sldiagviewer doesn't collect the errors when using the sim function however. Weird and why doesn't the sim command return errors? So I use the set_param function to 'push' the run button. That collects in the diary file but one of simulink's annoying features is that the .m file doesn't wait for the sim command to complete (please don't suggest a pause timer) so when I go to close, the close_system fails. I do see a 'wait' associated w/yet another rabbit hole known as Simulink.Simulation.Job but I gotta think there's a better way.
Thanks,
-Chris
0 Comments
Accepted Answer
Paul
on 10 Jun 2023
Using the sim command, simulation warnings are captured in the SimulationMetaData.ExecutionInfo of the output of the sim command. I ran a simulation that generated two divide by zero warnings:
>> out = sim('untitled');
>> out.SimulationMetadata.ExecutionInfo.WarningDiagnostics.Diagnostic
ans =
MSLDiagnostic with properties:
identifier: 'Simulink:blocks:DivideByZero'
message: 'Division by zero in 'untitled/Math Function''
paths: {'untitled/Math Function'}
cause: {}
stack: [0×1 struct]
ans =
MSLDiagnostic with properties:
identifier: 'Simulink:blocks:DivideByZero'
message: 'Division by zero in 'untitled/Math Function1''
paths: {'untitled/Math Function1'}
cause: {}
stack: [0×1 struct]
I'm pretty sure that error messages will be captured in the ErrorDiagnostics field
>> out.SimulationMetadata.ExecutionInfo
ans =
struct with fields:
StopEvent: 'ReachedStopTime'
StopEventSource: []
StopEventDescription: 'Reached stop time of 10'
ErrorDiagnostic: []
WarningDiagnostics: [2×1 struct]
4 Comments
Paul
on 12 Jun 2023
Or maybe a try/catch block will suffice?
>> try,sim(gcs),catch ME, ME, end
ME =
MSLException with properties:
handles: {1×0 cell}
identifier: 'MATLAB:MException:MultipleErrors'
message: 'Error due to multiple causes.'
cause: {2×1 cell}
stack: [0×1 struct]
Correction: []
>> ME.cause
ans =
2×1 cell array
{1×1 MSLException}
{1×1 MSLException}
>> ME.cause{1}
ans =
MSLException with properties:
handles: {[4.0360e+03]}
identifier: 'Simulink:DataType:InputPortDataTypeMismatch'
message: 'Data type mismatch. 'Input Port 1' of 'checkersim/Out1' expects a signal of data type 'uint32'. However, it is driven by a signal of data type 'single'.'
cause: {}
stack: [0×1 struct]
Correction: []
>> ME.cause{2}
ans =
MSLException with properties:
handles: {[2.2090e+03]}
identifier: 'Simulink:DataType:OutputPortDataTypeMismatch'
message: 'Data type mismatch. 'Output Port 1' of 'checkersim/In1' is a signal of data type 'single'. However, it is driving a signal of data type 'uint32'.'
cause: {}
stack: [0×1 struct]
Correction: []
More Answers (0)
See Also
Categories
Find more on Manual Performance Optimization in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!