For loop to perform multiple model simulations, error avoidance through try/catch

1 次查看(过去 30 天)
Hi.
I'm performing the tuning of some parameters in my simulink model, by using a for loop and simulating for each set of parameters.
When the system is unstable, the simulation ends before the stop time, causing an error.
To skip the error message, I used try/catch:
...
try
out = sim("model.slx");
catch
end
...
However, one important information for me is how far the simulation progresses for each set of parameters. The try/catch block does not save the "out" variable in the presence of an error, meaning that "out.tout" is not updated for each simulation.
How can I keep running the for loop, skip errors and update the out variable?
Are there any alternatives to try/catch for my specific case?

采纳的回答

Harsh Saxena
Harsh Saxena 2023-7-17
Hi Vita,
You can use the try/catch statement like this:
try
out = sim("model.slx");
catch ME
disp(ME);
out = ME;
end
Using this you will be able to see the error message corresponding to every simulation as well as store it in out as well. I would recommend storing it like:
catch ME
disp(ME);
out = [out;ME];
end
to store all the error messages and not overwrite any one of them.
Hope this helps!

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

产品


版本

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by