- table detected. Table variables can be cell arrays, so should the return be (for example) 'myinfo.cheeses.brie(:,3)' (all rows of variable number 3), or should it be 'myinfo.cheeses.brie.flavor' (variable name within table), or should it be the list {'myinfo.cheeses.brie{1,3}', 'myinfo.cheeses.brie{2,3}', ...} ?
- non-scalar struct detected. When the indices are emitted as part of the field names, is linear indexing acceptable or should explicit subscripts be used? 'mycell.cheeses(5,2).brie' or is 'mycell.cheeses(11).brie' be acceptable?
- graphics objects detected. Is it enough to just look at the most obvious UserData, or should all appdata be examined (for example, guidata() is implemented as the UsedByGUIData_m property)? Should the entire hierarchy be exampled, so for example the title() property of a subplot might have UserData that contains a cell array
- other kinds of objects might have properties that are cell arrays. But those properties are not necessarily public properties. For example you might have stored the result of detectImportOptions and inside that there would be an array of variable options, and variable options can contain cell arrays. If you attempt to access fields directly instead of using the functions, the code will complain.
Find field which are cells arrays within a Complex Structure
조회 수: 3 (최근 30일)
이전 댓글 표시
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
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 Center 및 File Exchange에서 Structures에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!