Functions are preferred to scripts.
Nested struct
s are used to hold data, so that the data is labeled explicitly
rather than relying on an indexing convention.
Fields in struct
s can be dynamically accessed with the
someStruct.(fieldName)
syntax; i.e.,
fieldName = 'someField';
someStruct.(fieldName) == someStruct.someField; % is true
and they can be iterated over in a loop like so:
fields = fieldnames(someStruct);
for i = 1:length(fields)
% Note the cell-array indexing with braces rather than parentheses
field = fields{i};
doSomethingWith(someStruct.(field));
end
or, more concisely,
for field = fieldnames(someStruct)
% This line is strange but necessary because MATLAB has awful semantics
field = field{1};
doSomethingWith(someStruct.(field))
end
The second construction is more readable, but doesn't work with parfor
loops.