Iterating through struct fieldnames in MATLAB

前端 未结 4 855
说谎
说谎 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条回答
  •  慢半拍i
    慢半拍i (楼主)
    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.

提交回复
热议问题