try - Execute statements and catch resulting errors - MATLAB (original) (raw)

The try block shows improved performance when the statements within the block run error-free. For example, this code is approximately 6x faster than in the previous release.

function testTryPerformance x = 1; for i = 1:1e8 try x = x * i; catch warning("Assignment was not successful.") x = 1; end end end

The approximate execution times are:

R2021b: 2.3 s

R2022a: 0.4 s

The code was timed on a Windows® 10, Intel® Xeon® CPU E5-1650 v4 @ 3.60 GHz test system using thetimeit function.

timeit(@testTryPerformance)