octave-maintainers
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: struct arrays and cs-lists


From: Jordi Gutiérrez Hermoso
Subject: Re: struct arrays and cs-lists
Date: Tue, 13 Nov 2012 09:42:54 -0500

On 13 November 2012 09:32, Ben Abbott <address@hidden> wrote:
>
> On Nov 13, 2012, at 8:06 AM, Jordi Gutiérrez Hermoso wrote:
>
>> On 13 November 2012 08:05, Jordi Gutiérrez Hermoso <address@hidden> wrote:
>>> On 13 November 2012 00:02, John W. Eaton <address@hidden> wrote:
>>>> On 12-Nov-2012, Jordi Gutiérrez Hermoso wrote:
>>>>
>>>> | I don't really understand why if s is a struct array with field
>>>> | foo, then "s.foo" is a cs-list. What's the purpose of this? How
>>>> | can you get something like a cell array instead without
>>>> | something as roundabout as resize(size(s), {s.foo})? Does it
>>>> | have to be this way, is there something else we can do instead?
>>>>
>>>> I don't see that we can do anything else if we want to preserve
>>>> Matlab compatibility.
>>>
>>> Sure, we can't change it, but how is it supposed to be used? Why
>>> is it like this at all? There must *some* method to the Madlab.
>>
>> By way of contrast, for cell arrays you can avoid cs-lists and get
>> subcells if you index with () instead of {}. Why is there nothing
>> equivalent for struct arrays?
>
> For cells indexing with () preserves the cell class just as it does
> for any data type (including structures). That's equivalent correct?
> ... So, its not clear to me what you want to do.

Consider the following:

    s = struct("foo", {1, "lol"}, "bar", {2, {2}})

then s.foo is a cs-list with two different types, as is s.bar.

> Are you looking to create a cell-array from a structure while
> maintaining the original cell-array's size, or something else.

Precisely this, I want the equivalent of

    reshape({s.foo}, size(s))

which isn't such a mouthful. I would expect something like s("foo") to
do this, indexing with () and strings, or s.("foo"), but the latter
syntax is already a synonym for s.foo.

The point is that I think struct arrays are best interpreted as cell
arrays where one dimension is indexed with strings instead of
positive integers. Indeed, the struct2cell and cell2struct functions
perform the conversion this way, by changing the way that one
dimension is indexed, whether with strings or positive integers. The
problem is that there seems to be no way to index struct arrays along
their string dimension in the same way that you can index cell arrays.

So, if the language doesn't have what I want, then what's the point of
s.foo returning cs-lists? How is this supposed to be used? It seems to
me like the most natural use of struct arrays is to do something like
a CSV file where the column headers turn into struct array field
names, but if indexing by those field names gives you cs-lists, then a
weird indirection appears in the middle.

So how are struct arrays meant to be used?

- Jordi G. H.


reply via email to

[Prev in Thread] Current Thread [Next in Thread]