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: Wed, 14 Nov 2012 10:46:29 -0500

On 14 November 2012 10:18, Yury T. <address@hidden> wrote:
> Jordi Gutiérrez Hermoso-2 wrote
>> I insist that the cell2struct solution I presented here solves your
>> problem:
>>
>>
>> http://octave.1599824.n4.nabble.com/equivalent-for-C-style-init-structname-varname-tp4646460p4646525.html
>>

> Also, this adaptation of your snippet doesn't work, saying "error:
> cell2struct: number of FIELDS does not match dimension":
>
> s = cell2struct(
>   {
>
>   "Buffat+Borel 1976 (5)", "Buffat+Borel 1976 (5)", 13, 1337;
>   "Lai+Guo+Petrova+ 1996 (2a)", "Lai+Guo+Petrova+ 1996 (2a)", 30, 505
>
>   },
>   { "ID", "data", "Xo", "Yo" }, 4
> );
>
> s(1)
>
> What am I doing wrong, then?

The third argument to cell2struct is the dimension along which you
want to replace indexing with integers to indexing with strings. In
this case, the second dimension (the column dimension). So the
following works:

s = cell2struct(
  {

  "Buffat+Borel 1976 (5)", "Buffat+Borel 1976 (5)", 13, 1337;
  "Lai+Guo+Petrova+ 1996 (2a)", "Lai+Guo+Petrova+ 1996 (2a)", 30, 505

  },
  { "ID", "data", "Xo", "Yo" }, 2
);

s(1)

s(2)

> ("for each item in array do smth.")

This can be done with the following family of functions:

    
http://www.gnu.org/software/octave/doc/interpreter/Function-Application.html#Function-Application

In addition, you an use Octave's usual built-in vector techniques:

    coords = [s.Xo; s.Yo];
    coords + 5
    norm (coords, "columns")

> and simple selective access to items by key of arbitrary type
> ("select item with ID=anything").

This sort of thing allows that:

    s(cellfun(@isempty, strfind({s.ID}, "Buffat")))

    s(cellfun(@isempty, strfind({s.data}, "Buffat")))

Look at the ingredients in there. First strfind to get the indices
where that "Buffat" string appears, the cellfun with @isempty to
convert to a boolean index, then using this index to index into s
again.

This sort of data manipulation is much easier in R, but at least it
can be done in Octave.

- Jordi G. H.


reply via email to

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