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 sort of third 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 Matlab empty arrays

An empty array for a particular Matlab data type is create by appending .empty to the data type name. Examples:

datetime.empty
string.empty
struct.empty

Then, the commonly used isempty() test works for any Matlab type.


function out = myfun(cfg)
arguments
  cfg struct = struct.empty
end

if isempty(cfg)
  cfg.lims = [0,100];
  cfg.out_dir = "~";
end

# ...