Find field which are cells arrays within a Complex Structure

3 vues (au cours des 30 derniers jours)
Paul Mitchell
Paul Mitchell le 12 Juin 2022
Commenté : Walter Roberson le 13 Juin 2022
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 commentaires
Paul Mitchell
Paul Mitchell le 13 Juin 2022
I did not think this was some petty point scoring forum. I will look elsewhere for an answer
Walter Roberson
Walter Roberson le 13 Juin 2022
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?

Connectez-vous pour commenter.

Réponses (0)

Catégories

En savoir plus sur Structures dans Help Center et File Exchange

Tags

Produits


Version

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by