Home > utils > emptyOrTextOrCellOfText.m

emptyOrTextOrCellOfText

PURPOSE ^

Validate [] OR text scalar (char row or string scalar) OR cell array of such text.

SYNOPSIS ^

function mustBeEmptyOrTextOrCellOfText(x)

DESCRIPTION ^

 Validate [] OR text scalar (char row or string scalar) OR cell array of such text.

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function mustBeEmptyOrTextOrCellOfText(x)
0002 % Validate [] OR text scalar (char row or string scalar) OR cell array of such text.
0003 
0004 % Allow explicit empty
0005 if isempty(x)
0006     return;
0007 end
0008 
0009 % Allow a single text scalar
0010 if ischar(x)
0011     if ~isrow(x)
0012         error('Text must be a character row vector, a string scalar, or empty.');
0013     end
0014     return;
0015 elseif isstring(x)
0016     if ~isscalar(x)
0017         error('Text must be a string scalar, a character row vector, or empty.');
0018     end
0019     return;
0020 end
0021 
0022 % Allow a cell array of text
0023 if iscell(x)
0024     for k = 1:numel(x)
0025         v = x{k};
0026         if ischar(v)
0027             if ~isrow(v)   % '' is 1x0 char, which is a row -> allowed
0028                 error('Each cell element must be a character row vector or a string scalar.');
0029             end
0030         elseif isstring(v)
0031             if ~isscalar(v)  % "" is a scalar string -> allowed
0032                 error('Each cell element must be a string scalar or a character row vector.');
0033             end
0034         else
0035             error('Each cell element must be text (string scalar or character row vector).');
0036         end
0037     end
0038     return;
0039 end
0040 
0041 % Anything else is invalid
0042 error('Value must be empty, a text value, or a cell array of text values.');
0043 end

Generated by m2html © 2005