What does the isempty function do in MATLAB / RunMat?
isempty(A) returns logical true when the MATLAB value A contains zero elements. It mirrors MATLAB's
behaviour across numeric arrays, logical arrays, character arrays, string arrays, cell arrays, structs,
GPU tensors, and handle-like values.
How does the isempty function behave in MATLAB / RunMat?
- Arrays are empty when any dimension is zero (
prod(size(A)) == 0). - Character arrays report empty when either dimension is zero. String scalars are never empty (
""is a1×1string array whose content may be empty, but it still counts as one element). - Cell arrays, struct arrays, and tables (future work) follow their MATLAB dimensions;
cell(0, n)is empty, whilecell(1, 1)is not. - Scalars, logical values, numeric scalars, function handles, objects, and handle objects always return
falsebecause they occupy one element. - GPU tensors rely on device-provided shape metadata to avoid unnecessary transfers. If metadata is missing, RunMat gathers once to confirm the shape.
isempty Function GPU Execution Behaviour
isempty does not launch GPU kernels. For GPU-resident tensors (gpuArray values), RunMat inspects the
shape stored in the GpuTensorHandle. When the active provider omits that metadata, the runtime downloads
the tensor once to maintain MATLAB-compatible results. The builtin always returns a host logical scalar and
does not allocate device memory, so it is safe in fused GPU expressions.
Examples of using the isempty function in MATLAB / RunMat
Checking if a matrix has any elements
A = zeros(0, 3);
tf = isempty(A);
Expected output:
tf = logical(1)
Detecting an empty cell array
C = cell(0, 4);
tf = isempty(C);
Expected output:
tf = logical(1)
Using isempty on a GPU-resident tensor
G = gpuArray(zeros(5, 0));
tf = isempty(G);
Expected output:
tf = logical(1)
Distinguishing char arrays from string scalars
chars = '';
tf_chars = isempty(chars);
str = "";
tf_str = isempty(str);
Expected output:
tf_chars = logical(1)
tf_str = logical(0)
Confirming that scalars are never empty
value = 42;
tf = isempty(value);
Expected output:
tf = logical(0)
Inspecting empty string arrays
S = strings(0, 2);
tf = isempty(S);
Expected output:
tf = logical(1)
FAQ
Does isempty gather GPU data?
Only when the provider fails to populate shape metadata on the GPU handle. Otherwise, it answers using the metadata and avoids transfers.
Why is isempty("") false but isempty('') true?
String scalars are 1×1 arrays that hold text content, so they are not empty even when the text has length
zero. Character arrays store individual characters; the empty char literal '' has zero columns, so it is
empty.
How does isempty treat structs and objects?
Structs follow their array dimensions. A scalar struct (1×1) is not empty, while a 0×0 struct array is
empty. Value objects and handle objects are always treated as scalar values, so isempty returns false.
Can isempty be used inside GPU-fused expressions?
Yes. The builtin returns a host logical scalar and does not allocate GPU buffers, so fusion plans remain valid.
Does isempty look inside cell arrays?
No. It only checks the container dimensions. To look at the contents, inspect individual cells.
What does isempty return for logical or numeric scalars?
They behave like any other scalar and return false.