Passing structure field name in function
3 ビュー (過去 30 日間)
古いコメントを表示
I have a function that accepts a structure and a field_identifier (character format) as inputs. The structure loaded has substructures in it, for which the field_identifier loaded in the function matches. The format is the following: structure(i).substructure.field_identifier
I have two checkpoints, ~isstruct and ~isfield for each of the inputs and it works fine. After that I initialize a for loop, defined as:
i=1;
for i=1:size(structure(i).substructure.field_identifier,1)
With this, I get the error that field_identifier is not a valid field. I understand the problem lies in the way I am passing the field identifier, but I have tried several things and cannot figure a way of passing field_identifiers as arguments for the for loop within this function. Can someone tell me what am I doing wrong?
Thanks in advance for the advice!
0 件のコメント
採用された回答
Stephen23
2016 年 6 月 9 日
編集済み: Stephen23
2016 年 6 月 9 日
structure(i).substructure.(field_identifier)
This is clearly explained in the documentation: http://www.mathworks.com/help/matlab/matlab_prog/generate-field-names-from-variables.html
Note that it is recommended to avoid using i and j for loop variable names, because these are names of the inbuilt imaginary unit.
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Structures についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!