num2cell

Puts each element of an array in its own cell.

Category: Language Fundamentals > Data Types

Syntax

C = num2cell(A)

Returns a cell array the same size as A, each cell holding one element.

Description

Each element becomes a 1-by-1 piece of content, with the class of A preserved. The result is handy for feeding a comma-separated list to a function, as in x = num2cell(v); f(x{:}), or for building a cell array from a matrix before editing individual cells.

The opposite conversion, from cells holding scalars back to a matrix, is cell2mat.

Input arguments

A
Array of any type: numeric, logical, char, string, cell or struct.

Output arguments

C
Cell array with numel(A) cells and the same size as A.

Example

C = num2cell([1 2; 3 4])
num2cell(["a", "b"])
Output
C = 2×2 cell

    {[1]}    {[2]}
    {[3]}    {[4]}
ans = 1×2 cell

    {["a"]}    {["b"]}

See also

cell2mat mat2cell cell struct2cell cellfun