help-octave
[Top][All Lists]
Advanced

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

Re: struct weirdness


From: Sergei Steshenko
Subject: Re: struct weirdness
Date: Fri, 24 Aug 2012 08:02:31 -0700 (PDT)


--- On Fri, 8/24/12, Ben Abbott <address@hidden> wrote:

> From: Ben Abbott <address@hidden>
> Subject: Re: struct weirdness
> To: "Sergei Steshenko" <address@hidden>
> Cc: "John W. Eaton" <address@hidden>, "Jordi Gutiérrez Hermoso" 
> <address@hidden>, "address@hidden" <address@hidden>
> Date: Friday, August 24, 2012, 4:31 AM
> 
> On Aug 24, 2012, at 5:23 AM, Sergei Steshenko wrote:
> 
> > --- On Thu, 8/23/12, John W. Eaton <address@hidden>
> wrote:
> > 
> >> From: John W. Eaton <address@hidden>
> >> Subject: Re: struct weirdness
> >> To: "Sergei Steshenko" <address@hidden>
> >> Cc: "Przemek Klosowski" <address@hidden>,
> "Jordi Gutiérrez Hermoso" <address@hidden>,
> "address@hidden"
> <address@hidden>
> >> Date: Thursday, August 23, 2012, 3:22 PM
> >> On 23-Aug-2012, Sergei Steshenko
> >> wrote:
> >> 
> >> | "This the square brackets should be curly" - if
> it's a
> >> must, and square brackets are accepted, then we
> have a bug -
> >> because no error message is issued.
> >> | 
> >> | If square brackets are allowed, then the struct
> element
> >> should be treated as vector in this case, which it
> is not -
> >> see my earlier Email in the thread. So, I think, we
> still
> >> have a bug - because square brackets are not
> treated as they
> >> normally do.
> >> 
> >> It's not a bug.
> >> 
> >> The behavior is compatible with Matlab.
> >> 
> >> The [...] argument is a single Matrix object, which
> is a
> >> single
> >> "scalar" element of a cell array.  So
> somewhat
> >> consistent with
> >> handling of scalar objects in other contexts, it
> is
> >> broadcast to all
> >> dimensions of the struct array.
> >> 
> >> jwe
> >> 
> > 
> > Which part of "help struct" explains that I can't apply
> 'size' operator to something that has been specified with
> '[...]' ?
> > 
> > Thanks,
> >  Sergei.
> 
> I think you're misunderstanding how structures work. 
> Consider "samples.age" ...
> 
> samples.age
> ans =
> 
>    45   52   45   23
> 
> ans =
> 
>    45   52   45   23
> 
> ans =
> 
>    45   52   45   23
> 
> ans =
> 
>    45   52   45   23
> 
> The four "ans" are due to the samples structure having size
> 1x4, and is the same as the comma separated list (cs-list)
> below.
> 
>     samples(1).age, samples(2).age,
> samples(3).age, samples(4).age
> 
> By placing cs-list between square brackets, the entire list
> is collected into a single variable.
> 
> age = [samples.age]
> age =
> 
>    45   52   45   23   45   52   45   23   45   52   45   23   45   52   45   
> 23
> 
> size (age)
> ans =
> 
>     1   16
> 
> Applying size() to [samples.age] give the expected result.
> 
> size ([samples.age])
> ans =
> 
>     1   16
> 
> Entering size(samples.age) is equivalent to ...
> 
>     size (samples(1).age, samples(2).age,
> samples(3).age, samples(4).age)
> 
> Which isn't compatible with the syntax for the size()
> function.
> 
> Ben
> 
> 
> 

Of course I misunderstand how 'struct' works - I'm supplying something through 
square brackets ('[...]') and 'size' refuses to work.

If, however, I supply this way:

foo.bar = [1 2 3]

, size does work.

Matlab/Octave behavior in case of 'struct' contradicts behavior of 
Perl/Python/C++/etc.

I.e. Matlab/Octave _discard_ the type I wanted it use by specifying '[...]', 
and even a warning isn't issued.

Regards,
  Sergei.

P.S. I suggest to introduce 'consistent_struct' that would work preserving 
type. i.e.

foo = consistent_struct
        (
        names, {"Peter", "Paul", "Mary"},
        ages, [27 26 25]
        );

should work as

foo.names = {"Peter", "Paul", "Mary"};
foo.ages = [27 26 25];

- no replication/broadcasting and other nonsense.


reply via email to

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