tags:

views:

150

answers:

3

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.

+7  A: 

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 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
gnovice
+2  A: 

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'}
Andrew Janke
+3  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
Jonas

related questions