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, 19 Nov 2012 12:45:11 -0500

On 19 November 2012 12:35, Dimitri Maziuk <address@hidden> wrote:
> On 11/19/2012 09:59 AM, Jordi Gutiérrez Hermoso wrote:
>
>> Right, if you have in C
>>
>>     typedef struct {
>>         const char* name;
>>         float value;
>>     } S;
>>
>>     S s = {"hello", 3.14},
>>
>>     printf("%s = %f\n", s.name, s.value);
>>
>> Then in Octave you can do,
>>
>>     s.name = "hello";
>>     s.value = single(3.14) ## You wanted single, not double, right?
>>
>>     printf("%s = %f\n", s.name, s.value);
>>
>> amongst other possible syntaxes for defining a struct.
>>
>> Struct arrays are similar in Octave and C too, except that Octave
>> allows a different way of indexing struct arrays that C doesn't.
>
> Yeah right. So how come
>     rc = [];
>     row = [];
>     resno = tbl( 1 ).idx;
>     for i = tbl
>         if( i.idx != resno )
>             row( 1 ) = resno;
>             row( 2 ) = i.resid;
>             if( (row( 3 ) + row( 4 )) > -200 )
>                  rc = [ rc ; row ];
>             endif
>             for j = 3 : 9
>                 row( j ) = -100.0;
>             endfor
>         endif
>         switch( toupper( i.atomid ) )
>             case "CA" ; row( 3 ) = i.val;
>             case "CB" ; row( 4 ) = i.val;
>             case "HA"
>                 row
>                 row( 5 )
>                 i
>                 i.val
>                 row( 5 ) = i.val;
> ...
>
> Output:
> ---------------------------------------------------
>
> octave:1> rc = pipe2lacs( "../88268_3.txt" )
> row =
>
>     49    14  -100  -100  -100  -100  -100  -100  -100
>
> ans = -100
> i =
>
>   scalar structure containing the fields:
>
>     idx = 2
>     resid =  14
>     atomid = HA
>     val = 4.410
>
> ans = 4.410
> error: pipe2lacs: A(I) = X: X must have the same size as I
> ------------------------------------------------
>
> this is on "row( 5 ) = i.val;" line... the fun part is I can break out
> into debug mode and type "row( 5 ) = i.val" and it will work without error.

You didn't give me enough context to debug this, and I suspect you
could be incorrect about the line in which this problem occurs, or you
could be looking at the wrong iteration of this loop. The error about
A(I) = X is completely unrelated to structs. My suspicion is that for
some i in tbl, it happens that i.val isn't a scalar, so you're
attempting to assign a vector or matrix to a single entry in the row
vector.

> Because map ("hash", red-black, doesn't matter) values are all of "class
> T" -- either strings or numbers, but not both.

The values can be any Octave type. The keys should be valid variable
names, unless we decide that we don't want to follow Matlab here at
all, but I think Octave should at least warn if we go this route.

- Jordi G. H.


reply via email to

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