Struct dynamic addressing syntax
4 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
Will provide the example to illustrate -- have two field names, say 'Income' and 'Expense' which are subaccounts of many parent accounts in a large table. The need is to locate the row positions of eac account subaccount and return in a struct an array of those locations. It is known the bounds of the main account for each as an array of indices, ix to the first/last record in the table. It is then easy to use that knowledge to find the two rows; a syntax such as
[i1,i2]=find(matches(table2array(tB(ix(1):ix(2),1:3)),flds));
S.(flds)=find(i1);
would be ideal IF (the provervbial "big if") the order of i1 above matched the position of the two field names in the order in which they happen to occur in the table (this is an application interacting with a Quickbooks database exported to Excel; the user can either deliberately or accidentally cause QB to reorder the fund order to alphabetical instead so it cannot be assumed the order is fixed). A 'stable' option on the matches function similar to unique would be most helpful here.
If that issue is resolved, then it would seem should be possible to assign the array of indices to the array of field names in the struct but I've found no syntax that will work to do that. The latter may simply be that I've not used MATLAB structures with more than the most naively written syntax owing to I've always run into something similar to this that has always frustrated me and caused me to use some other storage strategem(*). So, now with a specific example, thought I'd throw it for discussion...
(*) For now, I've reverted to the simple for...end loop over the fieldnames but it just seemed as though something like the original should be doable.
6 Commenti
Peter Perkins
il 12 Dic 2022
dpb, like Steve, I'm having trouble understanding the questions. Especially I'm not following
"A 'stable' option on the matches function similar to unique would be most helpful here", because matches returns a logical the same size as its first input, so in that sense, it is stable.
and
"assign the array of indices to the array of field names in the struct". You have a scalar struct, and there's no simple way to assign to multiple fields of a struct in one assignment (one of the reasons tables are often a better choice than struct for data that can be flattened).
Risposte (0)
Vedere anche
Categorie
Scopri di più su Structures in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!