Find field which are cells arrays within a Complex Structure

조회 수: 3 (최근 30일)
Paul Mitchell
Paul Mitchell 2022년 6월 12일
댓글: Walter Roberson 2022년 6월 13일
Hi, I have a complex structure in which some of the sub - sub - sub.... fields maybe cell arrays. However Simulink does to handle cell arrays. so need to convert these cell elements to structures within the origonal structure - or anything else simulink can handle.
My question. How can one easily search a complex structure for fields which are cells
Returning the fieldnames where the cell array begins
Note, These structures can be upto <n> fields deep in some instances (<n> varying from 2 to 10) and the exact nature of the structure is unknown until sampled
Thank you
Regards
Paul
  댓글 수: 5
Paul Mitchell
Paul Mitchell 2022년 6월 13일
I did not think this was some petty point scoring forum. I will look elsewhere for an answer
Walter Roberson
Walter Roberson 2022년 6월 13일
I got part way through an implementation, but I realized that I needed answers to those questions to proceed.
You said that your structure is complex. You did not restrict to scalar struct fields that are either numeric or logical or string or character or cell arrays. Any programmer working from the given specification would have to stop and ask what the desired output is for those different composite data types.
Another question:
It is significantly easier to implement this functionality using a recursive function (provided that there are no more than 50 levels.) The natural interface would be to have the user pass in the top level struct; if that is done by unindexed name, then the name of the variable can be detected inside the function.
This leads to the design question of whether the desired outputs should include the top level name, or if the desired outputs should only include the field names (and appropriate indices) ? Your question asked for the fieldnames, but the 'myinfo.cheeses.brie' is an example of including the top level variable name.
At the moment, I am not clear as to how you would use the field names in dot (and possibly indexed) form ? Unless you are planning to eval() to make changes? If you are planning to setfield then you would need to split at the dots and indices, so if you are planning to setfield() it would seem to make more sense to return setfield() compatible indices.
Would it perhaps make more sense to skip the phase of locating and returning the field names, in favour of writing a routine that went through a struct and did cell2struct() as it went, returning the modified struct ? A challenge for that, though, is that cell2struct() needs to be told the field names to use; did you have an algorithm in mind for how to name the new fields that replace the cell arrays?

댓글을 달려면 로그인하십시오.

답변 (0개)

카테고리

Help CenterFile Exchange에서 Structures에 대해 자세히 알아보기

태그

제품


릴리스

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by