append new fieldnames with values to structure in matlab

To append new fieldnames with values to a structure in MATLAB, you can use the dot notation.

Suppose you have a structure named myStruct and you want to add a new field newField with value newValue, you can use the following syntax:

main.m
myStruct.newField = newValue;
30 chars
2 lines

For example, if myStruct already has a field named field1, you can add a new field named newField with value 10 as follows:

main.m
myStruct.field1 = 5;
myStruct.newField = 10;
45 chars
3 lines

You can also add multiple fieldnames and values using the same dot notation. For example, you can add two new fields newField1 and newField2 with values newValue1 and newValue2, respectively, as follows:

main.m
myStruct.newField1 = newValue1;
myStruct.newField2 = newValue2;
64 chars
3 lines

Note that when you add new fields to a structure, MATLAB automatically expands the structure to accommodate the new fields.

gistlibby LogSnag