[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 08:31:39 -0800 (PST) |
>________________________________
> From: Yury T. <address@hidden>
>To: address@hidden
>Sent: Monday, November 12, 2012 6:09 PM
>Subject: equivalent for C-style init: structname varname[] = {...} ?
>
>Hi all
>
>I can't find or figure out an equivalent for the C-style variable
>initialization in Octave:
>
>structname varname[] = { {<field1_value>,<field2_value>},
>{<field1__value>,<field2_value>} };
>
>This example of what I'd like to do in Octave is assuming structname having
>two fields and the size of the array in structname units is defined by the
>number of constant initializers.
>
>Or are there good replacements for this sort of constructs, perhaps?
>
>Please advise.
>
>
>
>--
>View this message in context:
>http://octave.1599824.n4.nabble.com/equivalent-for-C-style-init-structname-varname-tp4646460.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
>
>
>
Have a look at the code below I once published to this list - maybe it's
close(r) to what you need.
Regards,
Sergei.
"
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) users 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.".
- equivalent for C-style init: structname varname[] = {...} ?, Yury T., 2012/11/12
- Re: equivalent for C-style init: structname varname[] = {...} ?, Jordi Gutiérrez Hermoso, 2012/11/12
- Re: equivalent for C-style init: structname varname[] = {...} ?,
Sergei Steshenko <=
- Re: equivalent for C-style init: structname varname[] = {...} ?, Jordi Gutiérrez Hermoso, 2012/11/12
- Re: equivalent for C-style init: structname varname[] = {...} ?, Yury T., 2012/11/12
- Re: equivalent for C-style init: structname varname[] = {...} ?, Jordi Gutiérrez Hermoso, 2012/11/12
- Re: equivalent for C-style init: structname varname[] = {...} ?, Yury T., 2012/11/12
- Re: equivalent for C-style init: structname varname[] = {...} ?, Jordi Gutiérrez Hermoso, 2012/11/12
- Re: equivalent for C-style init: structname varname[] = {...} ?, Yury T., 2012/11/12
- Re: equivalent for C-style init: structname varname[] = {...} ?, Jordi Gutiérrez Hermoso, 2012/11/12
- Re: equivalent for C-style init: structname varname[] = {...} ?, Yury T., 2012/11/12
- Re: equivalent for C-style init: structname varname[] = {...} ?, Jordi Gutiérrez Hermoso, 2012/11/12
- Re: equivalent for C-style init: structname varname[] = {...} ?, Sergei Steshenko, 2012/11/12