create a 1x5 struct in matlab

You can create a 1x5 struct in MATLAB using the following code:

main.m
s = struct('field1', {}, 'field2', {}, 'field3', {}, 'field4', {}, 'field5', {});
82 chars
2 lines

In this code, we are using the struct function to create an empty struct, and specifying the field names by including them as arguments with empty brackets {}. We can specify any field names we like to replace field1, field2, etc.

Alternatively, you could create the struct first and add fields with the setfield or setfield functions:

main.m
s(5) = struct; % creates a 1x5 array of empty structs
s = setfield(s(1), 'field1', []); % sets field1 of first struct to empty array
133 chars
3 lines

Here, we create an array of 5 empty structs first, then use setfield to set the field name and initial value of the first struct.

gistlibby LogSnag