[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Struct arrays, column names, syntactic sugar...
From: |
Jaroslav Hajek |
Subject: |
Re: Struct arrays, column names, syntactic sugar... |
Date: |
Fri, 9 Apr 2010 21:18:46 +0200 |
On Fri, Apr 9, 2010 at 6:50 PM, forkandwait <address@hidden> wrote:
> Is there a way to reference groups of fields in a struct array by name?
>
> For example, consider A:
>
> A = cell2struct ({'Peter', 'Hannah', 'Robert';
> 185, 170, 168},
> {'Name','Height'}, 1);
> A(1)
>
> We can do A(1) and get Peter's data, or we could do [A(:).Name] and (sort of)
> get a list of names (though I would like a column of text rather than a
> concatenated row), but it would be super awesome to also be able to do
> something
> like
>
> A(1:2).(Name,Age)
>
> and get a 2 x 2 cell array with the appropriate data.
>
> Any thoughts? Are there common tricks to do this?
>
> The ONLY thing I miss about R is being able to reference heterogeneous data
> through lists of column names....
>
> Tx
>
You can use struct2cell and then normal indexing.
I thought about simply allowing a cell of strings as a dynamic field
index, but it doesn't seem to be that simple. The problem is that
x(i,j).(y) is supposed to yield a cs-list with number of elements
equivalent to numel (x, i, j) (this is how subsasgn calls are
handled). So, if y could be a cell of strings yielding a struct
instead, it would break consistency.
I think what would possibly suffice is a function that takes a
structure and extract only specified field, i.e. a counterpart of
rmfield.
--
RNDr. Jaroslav Hajek, PhD
computing expert & GNU Octave developer
Aeronautical Research and Test Institute (VZLU)
Prague, Czech Republic
url: www.highegg.matfyz.cz