MATLAB Answers

How can i force a new simulation if there is a warning

1 view (last 30 days)
Hello, I'm doing a lot of simulations on simulink, and i want to stop and skip the simulations when there is a warning. Is there a way to do it ?
This is the code i tried for the moment, but it doesn't skip the simulation, which takes a lot of time.
for p = 600 : 25 : 1600
for d = 250 : 10 : 300
for i = 0 : 2 : 20
Kp = p*eye(6);
Kd = d*eye(6);
Ki = i*eye(6);
sim controle_simple
catch ME
% some code to save data


Sign in to comment.

Accepted Answer

Jesús Zambrano
Jesús Zambrano on 15 Jul 2020
The commands try/catch are used for executing statements and catch errors messages not warning messages.
Since getting a warining can still allow you to run the model, you have to catch the specific warning message and include a 'break' in your for-loop.
A way to catch the warning message is by looking at the following variable when running a model:
For example, when a model has a unconnected outport, the value of this variable is
Then, your code will include something like:
if (strcmp(ans.SimulationMetadata.ExecutionInfo.WarningDiagnostics.Diagnostic.identifier,'Simulink:Engine:InputNotConnected'))
Hope it helps!


Show 1 older comment
Jesús Zambrano
Jesús Zambrano on 15 Jul 2020
Yes, it will get out of your for loop. Actually, since it is a warning, it will complete the current simulation, evaluate the warning message and then will stop your for loop. But you want to stop the current simulation if it has a warning. So... let's see:
Another way could be to go to the Configuration Parameters, section 'Diagnotics', where you will see different settings with values 'none', 'warning', 'error'. Identify the one that gives you warning message and set it as an 'error'. With this done, then you can use the try/catch commands, evaluate the ME.identifier in an 'if-else' and use the 'continue'.
Here you can find some examples:

Sign in to comment.

More Answers (0)


Community Treasure Hunt

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

Start Hunting!

Translated by