help-octave
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: cs-lists, structure arrays ....


From: David Bateman
Subject: Re: cs-lists, structure arrays ....
Date: Tue, 05 Apr 2005 12:36:45 +0200
User-agent: Mozilla Thunderbird 0.8 (X11/20040923)

Shai Ayal wrote:

Hi all,

I'm getting a bit lost in the list/cell/structure constructs. My data is most naturally adapted to be in a array of structures:

data(1).name="Shai"
data(1).height=189
data(1).weight="undisclosed"

data(2).name="....

now, how do I extract the heights of everyone ?

data(:).height gives me a "cs-list" type which is no-where documented and I can't seem to convert it to a vector (or in fact to anything). The only way I found is using a for loop on the structure array

hh=zeros(1,length(data));
for i=1:length(data), hh(i)=data(i).height; endfor

but this is not very "comfortable".

Also, if you have any suggestions on better data-structures to hold this kind of data feel free

Shai




With 2.9.1 I have

octave:1> data(1).name="Shai"; data(1).height = 189; data(1).weight = "undisclosed";
octave:2> data(2).name="David"; data(2).height = 175; data(2).weight = 70;
octave:3> data(:).name
ans =

(,
 [1] = Shai
 [2] = David
,)

as you say this is a cs-list (see the round brackets). Now consider the matlab way of doing this

>> data(1).name='Shai'; data(1).height = 189; data(1).weight = 'undisclosed';
>> data(2).name='David'; data(2).height = 175; data(2).weight = 70;
>> data(:).name

ans =

Shai


ans =

David

>> x = cell (size(data)); [x{:}] = data(:).name

x =

   'Shai'    'David'

We can't yet do "[x{:}] = ..." in octave and in any case it doesn't seem that "[x, y] = ..." is valid either. I think octave should allow "[a0, a1, ...] = cs_list"... However there is another way, consider "[data.height]"

octave:4> [data.height]
ans =

 189  175

which also works in matlab... However
octave:5> [data.name]
ans = ShaiDavid
octave6> [data(:).name]
ans = ShaiDavid

which gives exactly the same result in matlab, which is frankly not that useful. So the only way I see to deal with strings in a structure array is if octave allowed "[a0, a1, ...] = cs_list" and frankly you'd also need "[x{:}] = cs_list". I'm not sure how much effort that would be to implement though...

Cheers
David




--
David Bateman                                address@hidden
Motorola Labs - Paris +33 1 69 35 48 04 (Ph) Parc Les Algorithmes, Commune de St Aubin +33 1 69 35 77 01 (Fax) 91193 Gif-Sur-Yvette FRANCE

The information contained in this communication has been classified as: [x] General Business Information [ ] Motorola Internal Use Only [ ] Motorola Confidential Proprietary



-------------------------------------------------------------
Octave is freely available under the terms of the GNU GPL.

Octave's home on the web:  http://www.octave.org
How to fund new projects:  http://www.octave.org/funding.html
Subscription information:  http://www.octave.org/archive.html
-------------------------------------------------------------



reply via email to

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