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

classic Classic list List threaded Threaded
114 messages Options
1234 ... 6
Reply | Threaded
Open this post in threaded view
|

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

Yury T.
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.
Reply | Threaded
Open this post in threaded view
|

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

Jordi Gutiérrez Hermoso-2
On 12 November 2012 11:09, Yury T. <[hidden email]> wrote:
> 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>} };

The struct command can do struct arrays:

    s = struct ("field1", {value1, value2}, "field2", {value3, value4})

Note that indexing a struct array with the field name will give you
cs-lists, however:

    s.field1
    s.field2

People often find cs-lists confusing because they cannot be assigned
to another variable and they get passed in a funny way in function
calls. If you cannot understand the explanation for cs-lists in the
manual, please let me know so we can improve the manual.

HTH,
- Jordi G. H.
_______________________________________________
Help-octave mailing list
[hidden email]
https://mailman.cae.wisc.edu/listinfo/help-octave
Reply | Threaded
Open this post in threaded view
|

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

Sergei Steshenko
In reply to this post by Yury T.
>________________________________
> From: Yury T. <[hidden email]>
>To: [hidden email]
>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
>[hidden email]
>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.".
_______________________________________________
Help-octave mailing list
[hidden email]
https://mailman.cae.wisc.edu/listinfo/help-octave
Reply | Threaded
Open this post in threaded view
|

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

Jordi Gutiérrez Hermoso-2
On 12 November 2012 11:31, Sergei Steshenko <[hidden email]> wrote:
> Have a look at the code below I once published to this list - maybe it's close(r) to what you need.

Just to clarify, Sergei wrote this code because he finds cs-lists too
confusing so he prefers to use scalar structs instead of struct
arrays. With scalar structs you avoid cs-lists. You may prefer scalar
structs, but you seemed to explicitly request struct arrays.

- Jordi G. H.
_______________________________________________
Help-octave mailing list
[hidden email]
https://mailman.cae.wisc.edu/listinfo/help-octave
Reply | Threaded
Open this post in threaded view
|

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

Yury T.
Thank you guys, but all this seems to be not I'm looking for.

Indeed, I'm looking for the arrays of structs and also I want to initialise those with undetermined number of structs-constants. Like I showed in my example, anyway.

Why do I need this? I have an undetermined number of datafiles which I need to process (fit with least-squares) using certain parameters specific to each file. And I'd like to have some annotations to those files, too (paper from which the data is derived etc.). The processing itself is quite uncomplicated, so I was figuring on use of something like following (C-style) right in my .m script:

name[] = {

{ "file1.dat", par1_1, par2_1, "ID1" }, # comment
{ "file2.dat", par1_2, par2_2, "ID2" }, # comment

};

which I would then expand (or contract) as necessary and just rerun the .m script. Previously I was doing just that sort of thing in R.

Of course, I might just have to go with a printf-format-style reading of the text file, but that will at the very least force me to use a separate file.

@Jordi: indeed, I feel that the documentation is somewhat unclear and not very happily structured w/r to the arrays of all kind. Don't get me wrong, you guys are doing sterling work, but...
Reply | Threaded
Open this post in threaded view
|

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

Jordi Gutiérrez Hermoso-2
On 12 November 2012 12:39, Yury T. <[hidden email]> wrote:
> @Jordi: indeed, I feel that the documentation is somewhat unclear and not
> very happily structured w/r to the arrays of all kind. Don't get me wrong,
> you guys are doing sterling work, but...

I don't like it either. Can you point to something specific that could
be improved?

- Jordi G. H.
_______________________________________________
Help-octave mailing list
[hidden email]
https://mailman.cae.wisc.edu/listinfo/help-octave
Reply | Threaded
Open this post in threaded view
|

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

Yury T.
So, no solution is forthcoming, eh? Pity.

What might be done with documentation primarily, I feel, is restructuring. Now, if I want to find something untrivial, I have to hunt all over the tree. Superficially, there is structure, but it is formed on somewhat unclear premises. Take the arrays (of which matrixes are a subset). Array is a complex data structure primarily characterised by many modes of addressing (indexing) its elements. Some modes are ubiquitous, from Basic to, as it is, Octave, some are not. Now, I feel, there is a traditional way to lay out the info on  language data types in documentation. You start with the general description, then turn to simple cases of vars definitions and constants, then to complex cases. Then to the typical use cases and to the special cases and exceptions. Only then you proceed with the operators and functions. And you never scatter the basic information across the manual. I've reread the Section 4, 5.2, and 6 just now, you know. :) Variables in Sec. 7? I have a feeling that the documentation was structured according to how the Octave was growing and being implemented... :)
Reply | Threaded
Open this post in threaded view
|

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

Jordi Gutiérrez Hermoso-2
On 12 November 2012 13:23, Yury T. <[hidden email]> wrote:
> So, no solution is forthcoming, eh? Pity.

No solution to what? Have you looked at the dataframe package in OF?
It's more like what you would find in R.

Also, struct arrays, cell arrays, and plain arrays are the basic types
in Octave. Plain arrays are of homogenous type, cell arrays are of
heterogenous type, where each cell can contain any Octave type,
including another cell, and a struct array is a cell array where one
dimension is indexed by strings instead of positive integers. These
are your basic building blocks, and perhaps they are enough for
solving your problem at hand. The cell() and struct() functions can be
used to build cell and struct arrays and ordinary arrays can be built
with [] notation. In addition, you can use {} to build cell arrays.

> What might be done with documentation primarily, I feel, is restructuring.
> Now, if I want to find something untrivial, I have to hunt all over the
> tree. Superficially, there is structure, but it is formed on somewhat
> unclear premises. Take the arrays (of which matrixes are a subset). Array is
> a complex data structure primarily characterised by many modes of addressing
> (indexing) its elements. Some modes are ubiquitous, from Basic to, as it is,
> Octave, some are not. Now, I feel, there is a traditional way to lay out the
> info on  language data types in documentation. You start with the general
> description, then turn to simple cases of vars definitions and constants,
> then to complex cases. Then to the typical use cases and to the special
> cases and exceptions. Only then you proceed with the operators and
> functions. And you never scatter the basic information across the manual.
> I've reread the Section 4, 5.2, and 6 just now, you know. :) Variables in
> Sec. 7? I have a feeling that the documentation was structured according to
> how the Octave was growing and being implemented... :)

Thanks, I will take this into consideration.

- Jordi G. H.
_______________________________________________
Help-octave mailing list
[hidden email]
https://mailman.cae.wisc.edu/listinfo/help-octave
Reply | Threaded
Open this post in threaded view
|

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

Yury T.
Jordi Gutiérrez Hermoso-2 wrote
On 12 November 2012 13:23, Yury T. <[hidden email]> wrote:
> So, no solution is forthcoming, eh? Pity.

No solution to what? Have you looked at the dataframe package in OF?
It's more like what you would find in R.
Well, thanks, but I was trying to move away from R and its pecularities, powerful as they might be.
Solution to my little problem, of course.

Jordi Gutiérrez Hermoso-2 wrote
Also, struct arrays, cell arrays, and plain arrays are the basic types
in Octave. Plain arrays are of homogenous type, cell arrays are of
...
dimension is indexed by strings instead of positive integers. These
are your basic building blocks, and perhaps they are enough for
solving your problem at hand. The cell() and struct() functions can be
...
Well, w/r to the theory of information, array is a complex data type, even if plain. That's how it's regarded in other programming languages - special syntax, complex indexing modes etc. BTW, even your explication on array constructors, so to speak, is better than what one finds in documentation. Sorry to grumble. Will try again. :)
Reply | Threaded
Open this post in threaded view
|

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

Sergei Steshenko
In reply to this post by Yury T.





----- Original Message -----

> From: Yury T. <[hidden email]>
> To: [hidden email]
> Cc:
> Sent: Monday, November 12, 2012 7:39 PM
> Subject: Re: equivalent for C-style init: structname varname[] = {...} ?
>
>T hank you guys, but all this seems to be not I'm looking for.
>
> Indeed, I'm looking for the arrays of structs and also I want to initialise
> those with undetermined number of structs-constants. Like I showed in my
> example, anyway.
>
> Why do I need this? I have an undetermined number of datafiles which I need
> to process (fit with least-squares) using certain parameters specific to
> each file. And I'd like to have some annotations to those files, too (paper
> from which the data is derived etc.). The processing itself is quite
> uncomplicated, so I was figuring on use of something like following
> (C-style) right in my .m script:
>
> name[] = {
>
> { "file1.dat", par1_1, par2_1, "ID1" }, # comment
> { "file2.dat", par1_2, par2_2, "ID2" }, # comment
>
> };
>
> which I would then expand (or contract) as necessary and just rerun the .m
> script. Previously I was doing just that sort of thing in R.
>
> Of course, I might just have to go with a printf-format-style reading of the
> text file, but that will at the very least force me to use a separate file.
>
> @Jordi: indeed, I feel that the documentation is somewhat unclear and not
> very happily structured w/r to the arrays of all kind. Don't get me wrong,
> you guys are doing sterling work, but...
>
>
>
>
> --
> View this message in context:
> http://octave.1599824.n4.nabble.com/equivalent-for-C-style-init-structname-varname-tp4646460p4646475.html
> Sent from the Octave - General mailing list archive at Nabble.com.
> _______________________________________________
> Help-octave mailing list
> [hidden email]
> https://mailman.cae.wisc.edu/listinfo/help-octave
>

Then isn't this:

"
octave:1> par1_1=1
par1_1 =  1
octave:2> par2_1=2
par2_1 =  2
octave:3> par1_2=3
par1_2 =  3
octave:4> par2_2=4
par2_2 =  4
octave:5> name = {{ "file1.dat", par1_1, par2_1, "ID1" }, { "file2.dat", par1_2, par2_2, "ID2" }}
name =
{
  [1,1] =
  {
    [1,1] = file1.dat
    [1,2] =  1
    [1,3] =  2
    [1,4] = ID1
  }
  [1,2] =
  {
    [1,1] = file2.dat
    [1,2] =  3
    [1,3] =  4
    [1,4] = ID2
  }
}
octave:6>               
"

as straightforward as it can be ?

--Sergei.
_______________________________________________
Help-octave mailing list
[hidden email]
https://mailman.cae.wisc.edu/listinfo/help-octave
Reply | Threaded
Open this post in threaded view
|

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

Jordi Gutiérrez Hermoso-2
In reply to this post by Yury T.
On 12 November 2012 14:25, Yury T. <[hidden email]> wrote:

> Jordi Gutiérrez Hermoso-2 wrote
>> On 12 November 2012 13:23, Yury T. &lt;
>
>> yury.tarasievich@
>
>> &gt; wrote:
>>> So, no solution is forthcoming, eh? Pity.
>>
>> No solution to what? Have you looked at the dataframe package in OF?
>> It's more like what you would find in R.
>
> Well, thanks, but I was trying to move away from R and its pecularities,
> powerful as they might be.

Oh, I think R is far better suited than Octave to handle data. The
following complaints are about Matlab, but they apply to Octave too:

    https://abandonmatlab.wordpress.com/2012/08/07/matlab-cant-read-plain-text-data-out-of-a-wet-paper-bag/

Dataframes are an attempt to make Octave less stupid about handling
hierarchical or categorised data.

> Solution to my little problem, of course.

I think I already gave you a solution using the struct function. But
here is another way using cell arrays:

    c = {

    { "file1.dat", par1_1, par2_1, "ID1" }, # comment
    { "file2.dat", par1_2, par2_2, "ID2" }, # comment

    };

It won't be very easy to index per field this way, but you can try
using cell2struct to turn your cell array into a struct array. This
function basically makes on dimension of the cell array be index with
strings.

> Jordi Gutiérrez Hermoso-2 wrote
>> Also, struct arrays, cell arrays, and plain arrays are the basic types
>> in Octave. Plain arrays are of homogenous type, cell arrays are of
>> ...
>> dimension is indexed by strings instead of positive integers. These
>> are your basic building blocks, and perhaps they are enough for
>> solving your problem at hand. The cell() and struct() functions can be
>> ...
>
> Well, w/r to the theory of information, array is a complex data
> type, even if plain.

Complex arrays are possible, of course, but they're usually real. In
Octave "array" means "n-dimensional array" and the standard type is
"matrix", which is of homogenous type. There are no 1d arrays, btw, so
"vectors", whether plain, cell or struct arrays, are always row or
column vectors.

I'm not sure that this has anything to do with information theory,
unless you're talking about how much entropy an array type holds, or
something like this.

Keep in mind that Octave was created to do linear algebra well and is
mostly a glorified wrapper to Netlib and LAPACK. Anything else that it
can do after that is an afterthought. In particular, it grew data
structures other than matrices later, much like Matlab itself.

> That's how it's regarded in other programming languages - special
> syntax, complex indexing modes etc.

You can't index with complex numbers in Octave, only with positive
integers or you can regard struct arrays as a special kind of array
where one dimension is indexed by strings.

> BTW, even your explication on array constructors, so to speak, is
> better than what one finds in documentation. Sorry to grumble. Will
> try again. :)

I'll add something like it to the manual, then.

- Jordi G. H.
_______________________________________________
Help-octave mailing list
[hidden email]
https://mailman.cae.wisc.edu/listinfo/help-octave
Reply | Threaded
Open this post in threaded view
|

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

Yury T.
In reply to this post by Sergei Steshenko
Would you please adapt my example to what you posted? I don't understand the multitude of Octave braces and parentheses too well.
Reply | Threaded
Open this post in threaded view
|

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

Jordi Gutiérrez Hermoso-2
On 12 November 2012 15:48, Yury T. <[hidden email]> wrote:
> Would you please adapt my example to what you posted? I don't understand the
> multitude of Octave braces and parentheses too well.

Whom are you talking to? What example?

- Jordi G. H.
_______________________________________________
Help-octave mailing list
[hidden email]
https://mailman.cae.wisc.edu/listinfo/help-octave
Reply | Threaded
Open this post in threaded view
|

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

Sergei Steshenko
In reply to this post by Yury T.




----- Original Message -----

> From: Yury T. <[hidden email]>
> To: [hidden email]
> Cc:
> Sent: Monday, November 12, 2012 10:48 PM
> Subject: Re: equivalent for C-style init: structname varname[] = {...} ?
>
> Would you please adapt my example to what you posted? I don't understand the
> multitude of Octave braces and parentheses too well.
>
>
>
> --
> View this message in context:
> http://octave.1599824.n4.nabble.com/equivalent-for-C-style-init-structname-varname-tp4646460p4646513.html
> Sent from the Octave - General mailing list archive at Nabble.com.
> _______________________________________________
> Help-octave mailing list
> [hidden email]
> https://mailman.cae.wisc.edu/listinfo/help-octave
>


http://octave.1599824.n4.nabble.com/equivalent-for-C-style-init-structname-varname-tt4646460.html#a4646510 ?

--Sergei.

_______________________________________________
Help-octave mailing list
[hidden email]
https://mailman.cae.wisc.edu/listinfo/help-octave
Reply | Threaded
Open this post in threaded view
|

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

Yury T.
In reply to this post by Jordi Gutiérrez Hermoso-2
Jordi Gutiérrez Hermoso-2 wrote
> Well, thanks, but I was trying to move away from R and its pecularities,
> powerful as they might be.

Oh, I think R is far better suited than Octave to handle data. The
following complaints are about Matlab, but they apply to Octave too:
Oh yes, R is a universe in itself and frankly, often it's one language too much. I don't use it often (mostly to generate complex plots) and always I spend too much time trying to remember how to do the simplest things. It's a boon if you may use just one language, even so special as R.

And I might have to abandon my moves away from it, for a while, as no *equivalent* solution presents itself.

But possibly Sergei will be so kind to adapt my example to that consistent_struct function he posted before. The 'cell structs' do not make good solution as they have no identifications for the individiual fields, as you pointed out, in fact. Or do they? Can those indices like in "[1,1] = file1.dat" be used without complex conversions?

And complaints about text files (CSVs, in fact)? Mighty OpenOffice balks at quite the simple CSV. CSV isn't easy.
Reply | Threaded
Open this post in threaded view
|

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

Yury T.
In reply to this post by Sergei Steshenko
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?
Reply | Threaded
Open this post in threaded view
|

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

Sergei Steshenko




----- Original Message -----

> From: Yury T. <[hidden email]>
> To: [hidden email]
> 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
> [hidden email]
> 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.
_______________________________________________
Help-octave mailing list
[hidden email]
https://mailman.cae.wisc.edu/listinfo/help-octave
Reply | Threaded
Open this post in threaded view
|

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

Sergei Steshenko




----- Original Message -----

> From: Sergei Steshenko <[hidden email]>
> To: Yury T. <[hidden email]>; "[hidden email]" <[hidden email]>
> Cc:
> Sent: Monday, November 12, 2012 11:34 PM
> Subject: Re: equivalent for C-style init: structname varname[] = {...} ?
>
>
>
>
>
> ----- Original Message -----
>>  From: Yury T. <[hidden email]>
>>  To: [hidden email]
>>  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
>>  [hidden email]
>>  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.
> _______________________________________________
> Help-octave mailing list
> [hidden email]
> https://mailman.cae.wisc.edu/listinfo/help-octave


Well, after thinking about it a little bit more - there is no sense to create structs with just one field.

So, probably Yuri wanted something like this:

"
octave:2> 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 = consistent_struct\
    23           (
    24           "file1.dat", {par1_1, par2_1, "ID1"},
    25           "file2.dat", {par1_2, par2_2, "ID2"}
    26           );
    27
    28
    29
    30  getfield(name, "file2.dat"){1}
    31  getfield(name, "file2.dat"){2}
    32  getfield(name, "file2.dat"){3}
octave:3> source("/home/sergei/junk/consistent_struct.m");
ans =  3
ans =  4
ans = ID2
octave:4> 
".

Regards,
  Sergei.



>
_______________________________________________
Help-octave mailing list
[hidden email]
https://mailman.cae.wisc.edu/listinfo/help-octave
Reply | Threaded
Open this post in threaded view
|

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

Jordi Gutiérrez Hermoso-2
In reply to this post by Yury T.
On 12 November 2012 16:14, Yury T. <[hidden email]> wrote:

> But possibly Sergei will be so kind to adapt my example to that
> consistent_struct function he posted before.

SS's "consistent_struct" is his misunderstanding of struct arrays,
which he refuses to attempt to understand due to the fact that
indexing them with fields produces cell arrays, which are anathema to
him. There's nothing consistent about it. In fact, it breaks array
semantics that all other Octave data types have.

> The 'cell structs'

There is no such thing. There are cell arrays and struct arrays. The
latter is what SS hates because SS doesn't understand them. As I
explained, all a struct array is, is a cell array in which one
dimension is indexed by strings. This is exactly what the cell2struct
function does, changes one dimension of a cell array to the field
names that you give it.

So here's another way to perhaps do what you want, which I'm still not
sure Ix understand:

    s = cell2struct(
    { field1value1, field2value1;
      field1value2, field2value2;
      field1value3, field2value3}', {"field1", "field2"});

Note that I had to transpose the cell array with the ' operator so that it
was 2x3 so that the two fields would correspond to the 2 in dimension
1, the default dimension. An equivalent form is

    s = cell2struct(
    { field1value1, field2value1;
      field1value2, field2value2;
      field1value3, field2value3}, {"field1", "field2"}, 2);

where you specify the dimension to replace with field names. Now that
s is a struct array, you can do "s.field1" to get "field1value1,
field1value2, field1value3" as a cs-list or s(2) to get a scalar array
with field1 = value2 and field2 = value2.

> do not make good solution as they have no identifications for the
> individiual fields, as you pointed out, in fact. Or do they? Can
> those indices like in "[1,1] = file1.dat" be used without complex
> conversions?

Oh, wait, "complex" means "complicated"? Sorry, I thought you were
talking about complex numbers (real and imaginary parts).

No, you can't use cell arrays if you want to index by fields.

> And complaints about text files (CSVs, in fact)? Mighty OpenOffice
> balks at quite the simple CSV. CSV isn't easy.

It is, in fact, quite easy. There is a csv2struct function out there,
but Octave's treatement of CSV is currently quite atrocious.

- Jordi G. H.
_______________________________________________
Help-octave mailing list
[hidden email]
https://mailman.cae.wisc.edu/listinfo/help-octave
Reply | Threaded
Open this post in threaded view
|

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

Sergei Steshenko




----- Original Message -----

> From: Jordi Gutiérrez Hermoso <[hidden email]>
> To: Yury T. <[hidden email]>
> Cc: [hidden email]
> Sent: Monday, November 12, 2012 11:42 PM
> Subject: Re: equivalent for C-style init: structname varname[] = {...} ?
>
> On 12 November 2012 16:14, Yury T. <[hidden email]> wrote:
>
>>  But possibly Sergei will be so kind to adapt my example to that
>>  consistent_struct function he posted before.
>
> SS's "consistent_struct" is his misunderstanding of struct arrays,
> which he refuses to attempt to understand due to the fact that
> indexing them with fields produces cell arrays, which are anathema to
> him. There's nothing consistent about it. In fact, it breaks array
> semantics that all other Octave data types have.
[snip]
>
> - Jordi G. H.
> _______________________________________________
> Help-octave mailing list
> [hidden email]
> https://mailman.cae.wisc.edu/listinfo/help-octave
>

Of course. I have no interest in understanding crappy Matlab/Octave language.

Instead I prefer to wrap the two mentioned languages constructs by functions which allow me to use familiar C/C++/Perl paradigm/idioms, specifically the Perl ones.

Matlab/Octave structs are associative arrays according to the documentation, and this exactly how I treat them.

--Sergei.

_______________________________________________
Help-octave mailing list
[hidden email]
https://mailman.cae.wisc.edu/listinfo/help-octave
1234 ... 6