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: Mon, 3 Sep 2012 02:17:35 -0700 (PDT)

--- On Sun, 9/2/12, Jordi Gutiérrez Hermoso <address@hidden> wrote:

> From: Jordi Gutiérrez Hermoso <address@hidden>
> Subject: Re: struct weirdness
> To: "Sergei Steshenko" <address@hidden>
> Cc: "address@hidden" <address@hidden>, "Przemek Klosowski" <address@hidden>
> Date: Sunday, September 2, 2012, 7:20 PM
> On 2 September 2012 16:26, Sergei
> Steshenko <address@hidden>
> wrote:
> > "by doubling up the curly braces" - you still don't
> get.
> 
> And do you get why the braces have to be doubled? Have you
> mastered
> the concept of struct arrays and cs-lists and broadcasting?
> I know
> it's a bit too much to take it all at once, but if you are
> able to
> understand the periodic table of operators[1] of your
> belovèd Perl,
> you can master this.
> 
> - Jordi G. H.
> [1] 
> http://glyphic.s3.amazonaws.com/ozone/mark/periodic/Periodic%20Table%20of%20the%20Operators%20A4%20300dpi.jpg
> 

"And do you get why the braces have to be doubled?" - sure.


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.

...

I don't use Perl6 - I do not know what your URL has to do with the issue.

...

Octave 'help struct' sucks - too little info. OTOH, in Matlab ( 
http://www.mathworks.com/help/techdoc/ref/struct.html ) there is an example 
with double curly braces:

"
...
Fields That Are Cell Arrays

To create fields that contain cell arrays, place the cell arrays within a value 
cell array. For instance, to create a 1-by-1 structure, type

s = struct('strings',{{'hello','yes'}},'lengths',[5 3])
s = 
   strings: {'hello'  'yes'}
   lengths: [5 3]
...
".

...

Here is an eval-less version of 'consistent_struct' (also attached) :
"
octave:1> system("cat -n /home/sergei/junk/consistent_struct.m");
     1  # copyright Sergei steshenko, 2012.
     2  # released under 3 clause BSD license.
     3  # tested under octave-3.6.2.
     4  # Matlab (tm) user are explicitly encouraged to to trivially modify the 
code ('#' -> '%') if they like it and wnat to use it.
     5  # the function is supposed to implement functionality similar to 
associative arrays in Perl/Python/C++
     6
     7  function os = consistent_struct(varargin)
     8    if(rem(length(varargin),2))
     9      error("there must be even number of input arguments");
    10    endif
    11
    12    os = [];
    13    for struct_field_number = 1:2:length(varargin)
    14      #fprintf(stderr, "key: %s\n", varargin{struct_field_number});
    15      key = varargin{struct_field_number}; # no check of 'key' 
correctness is performed
    16      val = varargin{struct_field_number + 1};
    17      os = setfield(os, key, val);
    18    endfor
    19  endfunction
    20
    21
    22  # comment out the following test cases if you want yo just use the 
function
    23  os = consistent_struct("one", [1 2 3], "two", [5 6 7 8; 9 10 11 12], 
"three", 33, "four", {"foo", 44, "bar"});
    24
    25  # Przemek Klosowski's test case:
    26  samples = consistent_struct\
    27              (
    28              "patient", {"Bob", "Kevin", "Bob" , "Andrew"},
    29              "age",     [ 45  ,  52    ,  45   ,  23     ],
    30              "protein", {"H2B", "CDK2" , "CDK2", "Tip60" },
    31              "tube"   , [ 3   ,  5     ,  2    ,  18     ]
    32              );
octave:2> source("/home/sergei/junk/consistent_struct.m");
octave:3> size(samples.age)
ans =

   1   4

octave:4> samples.age(2)
ans =  52
octave:5> samples
samples =

  scalar structure containing the fields:

    patient = 
    {
      [1,1] = Bob
      [1,2] = Kevin
      [1,3] = Bob
      [1,4] = Andrew
    }
    age =

       45   52   45   23

    protein = 
    {
      [1,1] = H2B
      [1,2] = CDK2
      [1,3] = CDK2
      [1,4] = Tip60
    }
    tube =

        3    5    2   18


octave:6> samples.patient{2}
ans = Kevin
octave:7> 
".

It is not at all clear from Octave's 'help setfield' that "setfield(os, key, 
val);" is a valid construct.

Matlab documentation ( http://www.mathworks.com/help/techdoc/ref/setfield.html 
) OTOH in very beginning says:

"setfield function creates the field and assigns the specified value. Pass 
field references as strings.".


Regards,
  Sergei.

Attachment: consistent_struct.m
Description: Text Data


reply via email to

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