Struct dynamic addressing syntax

4 visualizzazioni (ultimi 30 giorni)
dpb
dpb il 30 Nov 2022
Commentato: dpb il 17 Dic 2022
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
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).
dpb
dpb il 17 Dic 2022
Sorry @Peter Perkins, I didn't do a good job at trying to make up a test case; too much time pressure to get results to the auditors that I didn't get back until now to even look...
I'll have to go back and see where/how the issue regarding ordering after the matches operation was an issue; that may have been a red herring that I didn't realize what had happened in the QB database about it having rearranged fields yet at the time...
I think the result to the Q? is in your last comment above "You have a scalar struct, and there's no simple way to assign to multiple fields of a struct in one assignment". That's the crux of what I was driving at because the result of the match is an array and storing those elements was what I was struggling with finding a convenient syntax for.
The overall structure is a table; this struct is a supporting data indexing structure that is the result of the lookup function, the fields of which are used to reference into the table by given rows across a group of individual funds. It has fields that are the components and the total for either a specific fund or across a group of funds which comprise either a single donor fund that may have multiple investment pieces or across a multitude of donor funds which comprise an "investment pool" across which investment earnings are allocated.
I used the struct there to have a concise mechanism in which to hold the indices to various pieces-parts that need for updating the table; it's possible a redesign there might be beneficial towards solving that problem. But, the main code now uses the struct and so it would take rewriting it to be able to make use of a different storage scheme for these...
The data are now at the auditors; I did get that completed; it's on to the large scholarship billing coding at the moment that is the present crisis. It provides the data against which of the various donor funds expenses are allocated to provide those to the students...and the cycle repeats.

Accedi per commentare.

Risposte (0)

Categorie

Scopri di più su Structures in Help Center e File Exchange

Prodotti

Community Treasure Hunt

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

Start Hunting!

Translated by