help-octave
[Top][All Lists]
Advanced

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

Re: struct weirdness


From: Jordi Gutiérrez Hermoso
Subject: Re: struct weirdness
Date: Tue, 4 Sep 2012 09:34:20 -0400

On 3 September 2012 05:17, Sergei Steshenko <address@hidden> wrote:
> --- On Sun, 9/2/12, Jordi Gutiérrez Hermoso <address@hidden> wrote:
> >  And do you get why the braces have to be doubled?
>
> Because Octave/Matlab language sucks, and you as a developer,
> instead of doing something simple like I did to make it less suck
> are all the time trying to drown me (and others) in the gory details
> of CS-lists.

My dear Seryozha,

The problem is that the very parts of the language that you refuse to
attempt to understand are precisely the parts that you're breaking
with your "consistent" struct. All you did with that function is make
it impossible to create struct arrays. But struct arrays are not
terrible enough to be forbidden from Octave.

Consider the following three cases,

A = struct(
           "patient", {"Bob", "Kevin", "Bob", "Andrew"},
           "age", {45, 52, 45, 23},
           "protein", {"H2B", "CDK2", "CDK2", "Tip60"},
           "tube", {3, 5, 2, 18}
           )';

B = cell2struct (
                 { "Bob", "Kevin", "Bob", "Andrew";
                  45, 52, 45, 23;
                  "H2B", "CDK2", "CDK2", "Tip60";
                  3, 5, 2, 18},
                 {"patient", "age", "protein", "tube"});

C = [struct("patient", "Bob", "age", 45, "protein", "H2B", "tube", 3);
     struct("patient", "Kevin", "age", 52, "protein", "CDK2", "tube", 5);
     struct("patient", "Bob", "age", 45, "protein", "CDK2", "tube", 2);
     struct("patient", "Andrew", "age", 23, "protein", "Tip60", "tube", 18)
     ];

As you can confirm yourself, isequal(A,B) and isequal(B,C) are both
true, so all three struct *arrays* are equal. I wrote B to explicitly
highlight how struct arrays are like cell arrays where one dimension
is indexed by strings (i.e. by the field names). I wrote C to
show how to construct a struct array from scalar structs using the
array-building syntax with square brackets. As you can see, size(A),
size(B), and size(C) are all equal, and they refer to the dimensions
of the struct *array*.

Would it help if I told you that struct evaluates "in cell context"
its arguments in even positions, to use the Perl language you favour?
Perhaps you will find this language more evocative.

Next consider the real advantage of a struct array:

    A(1)
    C(3)

give you all of the data pertaining to a single subject (his name,
age, protein, and test tube). You can naturally also index with all of
the usual Octave indexing constructs:

    A(end)
    B(2,1)

There is no simple way to do this slicing with the scalar structs you
so favour.

Alas, there is a drawback, Sergiyko. When you try to index a struct
array by its field elements, your old foe, cs-lists, make an
appearance:

    A.patient
    B.age

I know you loathe cs-lists, because you cannot assign them to a
variable. You also cannot pass them as a variable into function calls,
which sadly breaks your cosmological interpretation for Octave. You
can, however, force them into cell arrays or plain arrays under some
circumstances:

    {C.patient}
    [B.tube]

and even combine these two and perform the usual indexing tricks

    B([B.age] > 40)

HTH,
- Jordi G. H.


reply via email to

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