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:06:36 -0500

On 19 November 2012 12:58, Dimitri Maziuk <address@hidden> wrote:
> On 11/19/2012 11:45 AM, Jordi Gutiérrez Hermoso wrote:
>
>> 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.
>
> Oh please. Try it on a matlab user, they might believe you. Replacing
> that with
>
> ...
> if( isnumeric( tbl( i ).val ) )
>     val = tbl( i ).val
> else
>     val = str2num( tbl( i ).val )
> endif
> row( 5 ) = i.val;
> ...
>
> makes it work just fine so it's rather clear which part is interpreted
> as what type.

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.  This can't work, you can't assign an array of
size 3 ("lol") to an array of size 1 (row(2)).Amusing enough, the
following does work:

    row = [1, 2, 3 4]
    r(2:end) = "lol" ## assign char array to a double subarray

A simple solution is to make row a cell array which can store
heterogenous data types or to figure out why the tmpl struct array is
sometimes getting numbers and why it's sometimes getting strings.

> I wish I could rewrite this piece of matlab expletive deleted in
> numpy/python instead of trying to get it to run in octave...

I heartily encourage you to abandon this piece of shit (expletive
restored) language. No sarcasm. I don't think Matlab is a very good
language. I work on Octave only so that all that free Matlab code out
there can also have a free interpreter, not because I want to promote
Matlab usage.

- Jordi G. H.


reply via email to

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