MATLAB: How to create a cell array with struct elements

cell arraysstructures

I want to create a 2×7 cell array. Each element of the cell will be an empty structure with a field name of "myfield". I can do it using nested loops:
for i=1:2
for j=1:7
mycell{i,j} = struct('myfield',[])
end
end
Is there any way to avoid the loops? It looks ugly in my code.

Best Answer

repmat({struct('myfield',{})}, 2, 7)
Note the correction of struct('myfield',[]) to struct('myfield',{}). The version you had, with [], is for a 1 x 1 struct with a field that is initialized to []. The version with {}, creates a 0 x 0 struct with the field defined in its template but with no content. An "empty structure" should have have 0 as one of its dimensions or else it is not actually empty.