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: Jordi Gutiérrez Hermoso
Subject: Re: equivalent for C-style init: structname varname[] = {...} ?
Date: Mon, 12 Nov 2012 16:42:08 -0500

On 12 November 2012 16:14, Yury T. <address@hidden> 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.


reply via email to

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