Iterating through struct fieldnames in MATLAB

前端 未结 4 746
说谎
说谎 2020-11-30 20:24

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

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

fields = fieldnames(teststru         


        
相关标签:
4条回答
  • 2020-11-30 21:08

    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
    
    0 讨论(0)
  • 2020-11-30 21:16

    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
    
    0 讨论(0)
  • 2020-11-30 21:23

    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.

    0 讨论(0)
  • 2020-11-30 21:26

    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'}
    
    0 讨论(0)
提交回复
热议问题