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 13:46:29 -0500

On 19 November 2012 13:35, Dimitri Maziuk <address@hidden> wrote:
> On 11/19/2012 12:06 PM, Jordi Gutiérrez Hermoso wrote:
>
>> Okay, that gives me enough information to diagnose the problem. It's
>> unrelated to structs, as I guessed.
>>
>> The problem is that (1) Octave arrays are of homogenous type (2)
>> strings are arrays of char type. So when you're doing
>>
>>     row = [1, 2, 3, 4];
>>     r(2) = "lol",
>>
>> you're attempting to assign an array of size 3 to a single location in
>> row, the 2nd entry.
>
> The way I see it I am trying to assign 4.410 to row(5) previously
> initialized to -100.0.

The way you see is not what Octave is seeing. You are not assigning
4.410, for some reason, you're attempting to assign "4.410". Elsewhere
you have previously made this a string instead of a number. This is
unrelated to structs.

> I'm storing 4.410 in a struct next to "HE", a struct is actually a map,
> and map values are indeed of homogenous type. Hence "4.410" interpreted
> as ['4', '.', '4', '1', '0'].

Struct values can be of any type

    s.foo = 42;
    s.bar = {"lol", "omg", "wtf"};
    s.baz = magic(3) + i*rand(3);
    typeinfo(s.foo)
    typeinfo(s.bar)
    typeinfo(s.baz)

> If it's any consolation, it's a generally undecidable problem inherent
> in all typeless languages: what should 'if( "42" == 42 )' evaluate to
> and should 'if( "42" == (42) )' evaluate to the same thing. Octave's not
> alone there (though it's the worst I've seen so far).

This behaviour happens because (1) you're comparing an array to a
scalar and (2) equality broadcasts across arrays.

Octave's strings are really inherited from Fortran strings,
multidimensional character arrays, so e.g. ["foo"; "bar"] is ok but
["foobar"; "baz"]; is an error because you can't have an array with
rows of different sizes. Instead you need {"foobar"; "baz"}, a cell
array, where each cell is its own character string.

- Jordi G. H.


reply via email to

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