Main Content

namedargs2cell

Convert structure containing name-value pairs to cell array

Description

example

C = namedargs2cell(S) converts a scalar structure array containing name-value pairs to a cell array containing the names and values. This function converts a 1-by-1 structure with n number of fields to a 1-by-2n cell array with interleaved names and values.

Examples

collapse all

Create a structure with the fields XLim, Color, and Box and assign values to each field. Use namedargs2cell to convert the structure to a cell array that interleaves the field names and the values.

S.XLim = [1,100];
S.Color = "red";
S.Box = "on";
C = namedargs2cell(S)
C = 1×6 cell array
    {'XLim'}    {1×2 double}    {'Color'}    {["red"]}    {'Box'}    {["on"]}

Write a function that accepts name-value pairs for specific axes object properties and impose additional restrictions on those property values.

Convert a name-value structure to a cell array containing interleaved names and values. Pass the cell array to the axes function, which accepts name-value pairs in a cell array. Return the handle to the axes object.

function a = myAxes(axesProps)
    arguments
        axesProps.XLim (1,2) {mustBeNumeric}
        axesProps.Color string {mustBeMember(axesProps.Color,["red","green","blue"])}
        axesProps.Box matlab.lang.OnOffSwitchState
    end
    nvCell = namedargs2cell(axesProps);
    a = axes(nvCell{:});
end

The output of the namedarg2cell function converts the axesProps name-value structure defined in the arguments block to an interleaved cell array of names and values. Pass the nvCell cell array to the axes function, which accepts name-value pairs as a cell array.

a = myAxes("XLim",[1,10],"Color","red","Box","on");

Input Arguments

collapse all

Name-value structure, specified as a scalar structure array.

Data Types: struct

Output Arguments

collapse all

Cell array of interleaved names and values, returned as a 1-by-2n cell array, where n is the number of fields in the input structure.

Data Types: cell

Introduced in R2019b