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: Sat, 1 Sep 2012 16:54:23 -0700 (PDT)

--- On Thu, 8/23/12, Przemek Klosowski <address@hidden> wrote:

> From: Przemek Klosowski <address@hidden>
> Subject: struct weirdness
> To: "address@hidden" <address@hidden>
> Date: Thursday, August 23, 2012, 7:46 AM
> I was looking at the example from http://wiki.octave.org/Cookbook:
> 
> samples = struct ("patient", {"Bob", "Kevin", "Bob" ,
> "Andrew"},
>                
>    "age",     [ 45 
> ,  52    ,  45   , 
> 23     ],
>                
>    "protein", {"H2B", "CDK2" , "CDK2",
> "Tip60" },
>                
>    "tube"   , [
> 3   ,  5     , 
> 2    ,  18     ]
>                
>    );
> 
> in Octave 3.6.2. I see that samples.patient returns the
> sequence of names, as I'd expect (ans = 'Bob, ans = Kevin,
> etc). However, samples.age returns the 'age' array four
> times. Also, samples(1)
> returns
> 
> scalar structure containing the fields:
>  patient = Bob
>      age =
> 45   52   45   23
>  protein = H2B
>     tube =  3    5   
> 2   18
> 
> rather than what I was hoping for (Bob, 45, H2B, 3).
> 
> This cookbook recipe looked like a neat idea for keeping
> track of complex data but is there a better way using
> struct() for this purpose? or is a newer version of Octave
> required?
> _______________________________________________
> Help-octave mailing list
> address@hidden
> https://mailman.cae.wisc.edu/listinfo/help-octave
> 

Hello,

please find the 'consistent_struct' function I was talking about.

The file is attached, and here a screen session demonstrating how it works:

"
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    for struct_field_number = 1:2:length(varargin)
    13      #fprintf(stderr, "key: %s\n", varargin{struct_field_number});
    14      key = varargin{struct_field_number}; # no check of 'key' 
correctness is performed
    15      val = varargin{struct_field_number + 1};
    16      eval(sprintf("os.%s=val;", key));
    17      # the above 'eval' is ugly, but I do not know how to do it better - 
I need some kind of 'add_field_value_pair_to_struct' function
    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> 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:4> samples.patient
ans = 
{
  [1,1] = Bob
  [1,2] = Kevin
  [1,3] = Bob
  [1,4] = Andrew
}
octave:5> samples.age
ans =

   45   52   45   23

octave:6> samples.protein
ans = 
{
  [1,1] = H2B
  [1,2] = CDK2
  [1,3] = CDK2
  [1,4] = Tip60
}
octave:7> samples.tube
ans =

    3    5    2   18

octave:8> size(samples.age)
ans =

   1   4

octave:9> size(samples.tube)
ans =

   1   4

octave:10> os
os =

  scalar structure containing the fields:

    one =

       1   2   3

    two =

        5    6    7    8
        9   10   11   12

    three =  33
    four = 
    {
      [1,1] = foo
      [1,2] =  44
      [1,3] = bar
    }

octave:11> samples.patient{2}
ans = Kevin
octave:12> samples.age(2)
ans =  52
octave:13> 
".

I hope that's what Przemek expected and I hope Jordi still enjoys my lack of 
understanding of CS lists.

There is a mixture of {...} and [...] objects that happily and peacefully live 
together, and nothing is unnecessarily replicated.

Everything behaves as expected in Perl/Python/C++ world.


Regards,
  Sergei.

Attachment: consistent_struct.m
Description: Text Data


reply via email to

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