フィルターのクリア

Accessing various field names deep in a structure.

12 ビュー (過去 30 日間)
babi psylon
babi psylon 2013 年 11 月 12 日
編集済み: Cristian Constantinescu 2019 年 2 月 25 日
hi
Image I want to access fieldnames one, two, three, four in structure A.B.C.D.E.F and so on, with each possible letter containing fieldnames one, two, three four, e.g. A.B.C.one, A.B.C.two. In one iteration, I might wanna get A.B.C.one, but in the next A.B.C.D.one. I could of course make switch cases like: case condition, then go to A.B.C.D.one, case ..., but I would like to parse to common part of the fieldnames, e.g. 'B.C.D.E' to A. like this: A.('B.C.D.E').one. As such, ('B.C.D.E') could be easily replaced by e.g. ('B.C.') and my code would be less redundant. In a post by Loren, I found the answer below to solve my problem, posted by Brad Stiritz. However, I don't get this to work, when I try
A.B.C.D = 50; ans = A.getField('A.B.C.D')
I get
Reference to non-existent field 'getField'.
I tried ans = getField(A,'B.C.D') and ans = getField(A,'A.B.C.D'), but also got errors. Anyone who has a similar solution or can get this to work?
Thx!
I cite: " I wrote a class ‘FieldInterface’ to encapsulate arbitrary-depth struct fields, just as you’re talking about, so I can do things like:
————————————–
A.setField(‘A.B.C.D’,20); ans = A.getField(‘A.B.C.D’);
————————————–
The code snip below is the essence of method getField(). Note the reference to classdef constant ROOT_FIELD_NAME, which anchors the “dynamic” Fields.
————————————–
function outValue = getField(obj,varargin) … fieldSpecStr = varargin{1}; … try
% Construct near-complete field name (only lacking ‘obj.’), e.g. % ‘ROOT_FIELD_NAME.subStructName.fieldName’ fullFieldStr = [ obj.ROOT_FIELD_NAME '.' fieldSpecStr ];
% Get cell vector of sub-struct / field names within (fullFieldStr) cvFieldSplits = regexp(fullFieldStr,’\.’,'split’);
% Initialize (outValue): outValue = obj;
% Iterate through (cvFieldSplits) for iSplit = 1 : numel(cvFieldSplits)
% Access next-level within (cvFieldSplits), which will either be a % further sub-struct or else the final, desired field: outValue = outValue.(cvFieldSplits{iSplit}); end
% Handle failure catch exception … end
————————————–
Hope s/o finds this useful. Any comments appreciated,
  3 件のコメント
Matt J
Matt J 2013 年 11 月 12 日
編集済み: Matt J 2013 年 11 月 12 日
It just seems like a bad idea to nest structs so deep. Most of the indexing you describe is basically what struct arrays were designed for. So, rather than having
one.two.three.two.one.F
you would instead make F a struct array and do F(1,2,3,2,1).

サインインしてコメントする。

採用された回答

babi psylon
babi psylon 2013 年 11 月 12 日

その他の回答 (1 件)

Paul Jordan
Paul Jordan 2018 年 11 月 14 日
A.B.C.D = 50;
s = {'B','C','D'};
getfield(A,s{:})
ans =
50
  1 件のコメント
Cristian Constantinescu
Cristian Constantinescu 2019 年 2 月 25 日
編集済み: Cristian Constantinescu 2019 年 2 月 25 日
This is such a concise and elegant solution to something I've been searching for a while.
Thank you!

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeStructures についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by