Iterating through struct fieldnames in MATLAB

My question is easily summarized as: "Why does the following not work?"

teststruct = struct('a',3,'b',5,'c',9)


fields = fieldnames(teststruct)


for i=1:numel(fields)
fields(i)
teststruct.(fields(i))
end

output:

ans = 'a'


??? Argument to dynamic structure reference must evaluate to a valid field name.

Especially since teststruct.('a') does work. And fields(i) prints out ans = 'a'.

I can't get my head around it.

105417 次浏览

You have to use curly braces ({}) to access fields, since the fieldnames function returns a cell array of strings:

for i = 1:numel(fields)
teststruct.(fields{i})
end

Using parentheses to access data in your cell array will just return another cell array, which is displayed differently from a character array:

>> fields(1)  % Get the first cell of the cell array


ans =


'a'       % This is how the 1-element cell array is displayed


>> fields{1}  % Get the contents of the first cell of the cell array


ans =


a             % This is how the single character is displayed

Your fns is a cellstr array. You need to index in to it with {} instead of () to get the single string out as char.

fns{i}
teststruct.(fns{i})

Indexing in to it with () returns a 1-long cellstr array, which isn't the same format as the char array that the ".(name)" dynamic field reference wants. The formatting, especially in the display output, can be confusing. To see the difference, try this.

name_as_char = 'a'
name_as_cellstr = {'a'}

Since fields or fns are cell arrays, you have to index with curly brackets {} in order to access the contents of the cell, i.e. the string.

Note that instead of looping over a number, you can also loop over fields directly, making use of a neat Matlab features that lets you loop through any array. The iteration variable takes on the value of each column of the array.

teststruct = struct('a',3,'b',5,'c',9)


fields = fieldnames(teststruct)


for fn=fields'
fn
%# since fn is a 1-by-1 cell array, you still need to index into it, unfortunately
teststruct.(fn{1})
end

You can use the for each toolbox from http://www.mathworks.com/matlabcentral/fileexchange/48729-for-each.

>> signal
signal =
sin: \{\{1x1x25 cell}  {1x1x25 cell}}
cos: \{\{1x1x25 cell}  {1x1x25 cell}}


>> each(fieldnames(signal))
ans =
CellIterator with properties:


NumberOfIterations: 2.0000e+000

Usage:

for bridge = each(fieldnames(signal))
signal.(bridge) = rand(10);
end

I like it very much. Credit of course go to Jeremy Hughes who developed the toolbox.