Matlab .empty array initialization
Many coding languages have objects that are useful as sentinels to indicate missing data or unused parameters.
For example, if a boolean parameter’s state has not been checked, it’s a bit disingenuous to say available = false
.
Sentinel values give a state that can communicate that something is unknown or unset.
Some example sentinels:
In Matlab an empty array can be used as a sentinel.
Create empty arrays for Matlab data types by appending .empty
to the data type name.
Examples:
datetime.empty
string.empty
struct.empty
The commonly used isempty()
works for any Matlab type:
function out = myfun(cfg)
arguments
cfg struct = struct.empty
end
if isempty(cfg)
cfg.lims = [0,100];
end