namedargs2cell

Converts a scalar structure into a cell array of name-value pairs.

Category: Programming > Functions

Syntax

C = namedargs2cell(S)

Returns a row cell array {name1, value1, name2, value2, ...} built from the fields of S.

Description

Many functions take options as trailing name-value pairs. Keeping the options in a structure is convenient for defaults and merging, and namedargs2cell turns that structure into the form the function expects. Expanding the result with a comma-separated list, f(x, C{:}), passes the options through.

The field names are returned as character vectors and the values are stored unchanged.

Input arguments

S
Scalar structure. Each field name becomes a name and its content the matching value.

Output arguments

C
Cell array of size 1-by-2n for a structure with n fields, in field order.

Example

Passing options stored in a structure

opts = struct('LineWidth', 2, 'Color', 'r');
C = namedargs2cell(opts)
plot(1:5, (1:5).^2, C{:})
Output
C = 1×4 cell

    {'LineWidth'}    {[2]}    {'Color'}    {'r'}

See also

struct2cell cell2struct fieldnames inputParser