Note that there are some explanatory texts on larger screens.

plurals
  1. POLogical indexing of fields within a structure
    primarykey
    data
    text
    <p>I have a structure like so:</p> <pre><code>Basis.FieldsBasisType.fieldsBasisComponents </code></pre> <p>There are ~13 components to each basis, including 6 asset class IDs.</p> <p>So, for example </p> <p>fieldnames(Basis.SalaryIncrease) =</p> <pre><code>'Constant' 'AWeight' 'AAssetClassID' 'ATimeLag' 'BWeight' 'BAssetClassID' 'BTimeLag' 'CWeight' 'CAssetClassID' 'CTimeLag' 'DWeight' 'DAssetClassID' 'DTimeLag' 'EWeight' 'EAssetClassID' 'ETimeLag' 'FWeight' 'FAssetClassID' 'FTimeLag' 'cap' 'floor' </code></pre> <p>Now what I want to do is select all unique asset classes used in any basis. I am really struggling to make this neat though, currently I am using:</p> <pre><code>basisNames = fieldnames(Basis); requiredSeries=[]; for i = 1:size(fieldnames(Basis),1) requiredSeries = [requiredSeries;unique(Basis.(basisNames{i}).AAssetClassID)]; requiredSeries = [requiredSeries;unique(Basis.(basisNames{i}).BAssetClassID)]; requiredSeries = [requiredSeries;unique(Basis.(basisNames{i}).CAssetClassID)]; requiredSeries = [requiredSeries;unique(Basis.(basisNames{i}).DAssetClassID)]; requiredSeries = [requiredSeries;unique(Basis.(basisNames{i}).EAssetClassID)]; requiredSeries = [requiredSeries;unique(Basis.(basisNames{i}).FAssetClassID)]; end requiredSeries = unique(requiredSeries) </code></pre> <p>Which is really ugly in my opinion. I want to do some kind of string compare to find 'AssetClassID' within the fields, so something like:</p> <pre><code>field = fieldnames(Basis.(basisNames{1})); strfind(field,'AssetClassID'); </code></pre> <p>And then use that cell array to logically index 'field' and just grab the data from 'AssetClassID' fields. But I am stuck on making that work.</p> <pre><code>~cellfun('isempty',strfind(field,'AssetClassID')) </code></pre> <p>gets me the logical index, how do I apply that to fields and then use it to get values. </p> <p>Any ideas would be appreciated, I feel there should be a neat way of doing it and I am missing something. Hardcoding those fieldnames seems short sighted as a solution.</p> # <p>Edit: I hate myself.</p> <p>Sorry folks, I came up with a working variant like moments after posting this, apologies for wasting anyones time!</p> <pre><code>basisNames = fieldnames(Basis); for i = 1:size(fieldnames(Basis),1) field = fieldnames(Basis.(basisNames{i})); field = cell2mat(field(~cellfun('isempty',strfind(field,'AssetClassID')))); for j = 1:size(field,1) requiredSeries = [requiredSeries;unique(Basis.(basisNames{i}).(field(1,:)))]; end requiredSeries = unique(requiredSeries) end </code></pre> <p>I was missing a necessary cell2mat earlier which caused the inability to get it to bloody work. Anyway, I'd always like to hear improvements to that but otherwise you can shut this down.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload