help-octave
[Top][All Lists]
Advanced

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

Re: equivalent for C-style init: structname varname[] = {...} ?


From: Sergei Steshenko
Subject: Re: equivalent for C-style init: structname varname[] = {...} ?
Date: Mon, 12 Nov 2012 13:34:56 -0800 (PST)




----- Original Message -----
> From: Yury T. <address@hidden>
> To: address@hidden
> Cc: 
> Sent: Monday, November 12, 2012 11:15 PM
> Subject: Re: equivalent for C-style init: structname varname[] = {...} ?
> 
> I've meant adapting my example for your consistent_struct.m. The 'cell
> structs' (which I tried) don't readily allow addressing the individiual
> fields, or do they?
> 
> 
> 
> 
> --
> View this message in context: 
> http://octave.1599824.n4.nabble.com/equivalent-for-C-style-init-structname-varname-tp4646460p4646520.html
> Sent from the Octave - General mailing list archive at Nabble.com.
> _______________________________________________
> Help-octave mailing list
> address@hidden
> https://mailman.cae.wisc.edu/listinfo/help-octave
>

Well, "come and get it" (c) :) :

"

octave:1> system("cat -n /home/sergei/junk/consistent_struct.m");
     1  function os = consistent_struct(varargin)
     2    if(rem(length(varargin),2))
     3      error("there must be even number of input arguments");
     4    endif
     5
     6    os = [];
     7    for struct_field_number = 1:2:length(varargin)
     8      #fprintf(stderr, "key: %s\n", varargin{struct_field_number});
     9      key = varargin{struct_field_number}; # no check of 'key' 
correctness is performed
    10      val = varargin{struct_field_number + 1};
    11      os = setfield(os, key, val);
    12    endfor
    13  endfunction
    14
    15  # testcase for Yury T - assuming Yuri wants to access things by 
"file1*.dat"
    16
    17  par1_1=1;
    18  par2_1=2;
    19  par1_2=3;
    20  par2_2=4;
    21
    22  name =\
    23    {
    24    consistent_struct("file1.dat", {par1_1, par2_1, "ID1"}),
    25    consistent_struct("file2.dat", {par1_2, par2_2, "ID2"})
    26    };
    27
    28
    29  name
    30
    31  getfield(name{2}, "file2.dat"){1}
    32  getfield(name{2}, "file2.dat"){2}
    33  getfield(name{2}, "file2.dat"){3}
octave:2>
octave:2> source("/home/sergei/junk/consistent_struct.m");
name =
{
  [1,1] =

    scalar structure containing the fields:

      file1.dat =
      {
        [1,1] =  1
        [1,2] =  2
        [1,3] = ID1
      }

  [2,1] =

    scalar structure containing the fields:

      file2.dat =
      {
        [1,1] =  3
        [1,2] =  4
        [1,3] = ID2
      }

}
ans =  3
ans =  4
ans = ID2
octave:3>      
"

Regards,
  Sergei.


reply via email to

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