Have you ever wanted to turn off the evaluation of "expensive" assert conditions after you have completed debugging your code to save CPU Cycles? Well then assertF is the function for you.
It has minimal overhead and supports the same inputs (errID, msg, A1,...,An) as MATLAB's assert function. It utilizes a "persistent" assertFlag variable that allows developers to set the flag (at the start of code execution) and as long as the assert condition calls include a function handle "@()", then the condition is not evaluted if the assertFlag was set to false. However, if a developer wants the condition to always be evaluated inpendent of the assertFlag (like the MATLAB assert function), then exclude the function handle from the condition (or always include the assertFlag as the 2nd variable - although this adds a touch more overhead)
There are four files that help demonstrate how to effectively use this function.
assertF.m: The assert function that enables an assertFlag to determine if conditions with function handles are evaluated.
Test_AssertCallTimes.m: Example file that demonstrates how expensive condition calls using assertF compare with MATLAB's assert function and how much time is saved when the assertFlag is set to false for the same conditions. This file also shows how to use assertF with and without function handles. To get accurate evaluation times we use MATLAB's timeit function.
Test_AssertFalseCases.m: Example file that has six different condition cases that are all false (and throw errors) and are caught in a try/catch block.
AssertFalseCase.m: The six cases called by Test_AssertFalseCases.m
引用格式
Dave Ober (2025). Function to avoid evaluation of assert condition (https://www.mathworks.com/matlabcentral/fileexchange/111865-function-to-avoid-evaluation-of-assert-condition), MATLAB Central File Exchange. 检索时间: .
MATLAB 版本兼容性
创建方式
R2022a
与 R2008a 及更高版本兼容
平台兼容性
Windows macOS Linux标签
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!