Structure arrays let you group related data with different types in one variable. This helps organize information clearly.
Structure arrays in MATLAB
structArray = struct('field1', {value1, value2, ...}, 'field2', {value1, value2, ...});
Each field holds data for all elements in the structure array.
Use curly braces {} to assign multiple values to fields for each element.
people = struct('name', {'Alice', 'Bob'}, 'age', {25, 30});
emptyStruct = struct('name', {}, 'age', {});
onePerson = struct('name', {'Charlie'}, 'age', {40});
people(3).name = 'Diana'; people(3).age = 22;
This program creates a structure array with two people, shows it, adds a third person, and shows the updated array.
people = struct('name', {'Alice', 'Bob'}, 'age', {25, 30}); disp('Before adding new person:'); disp(people); people(3).name = 'Charlie'; people(3).age = 40; disp('After adding new person:'); disp(people);
Accessing fields is fast because data is organized by field names.
Adding elements at the end is easy, but inserting in the middle requires shifting elements.
Use structure arrays when you want to keep related but different types of data together.
Time complexity for adding an element at the end is O(1), but inserting in the middle is O(n).
Space complexity grows with the number of elements and fields.
Common mistake: Mixing cell arrays and structure arrays syntax can cause errors.
Structure arrays group related data with different types in one variable.
Use them to organize data by named fields for easy access.
You can add, access, and modify elements using field names and indices.