convert name value pair in cell array to struct in matlab

You can convert a cell array of name-value pairs to a structure using the struct function.

Assuming you have a cell array myCell with name-value pairs where each name is a string and each value is a scalar:

main.m
myCell = {'name1', 1, 'name2', 2, 'name3', 3};
47 chars
2 lines

You can convert it to a structure using:

main.m
myStruct = struct(myCell{:});
30 chars
2 lines

This will create a structure with fields corresponding to the names and values corresponding to the values in the original cell array:

main.m
myStruct = 
    name1: 1
    name2: 2
    name3: 3
51 chars
5 lines

related categories

gistlibby LogSnag