help-octave
[Top][All Lists]
Advanced

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

Re: Arrays of structures


From: PhilipNienhuis
Subject: Re: Arrays of structures
Date: Tue, 31 Dec 2019 10:35:15 -0600 (CST)

Windhorn, Allen E [ACIM/LSA/MKT] wrote
> Supposing I have an array of structures X, where each element of the array
> has fields .th and .r.  I can refer to a particular field like "X(3).r" if
> I want to
> know its value, but is there a way to refer to ALL the r fields in the
> array as
> a group, like "X(:).r"?  This just gets me the value of the r field of the
> first
> element of the array for some reason:
> 
>>> X(1).th = 23;
>>> X(1).r = 10.3;
>>> X(2).th = 17;
>>> X(2).r = 6.6;
>>> X(3).th = 47;
>>> X(3).r = 11.5;
>>> X(:).r  % (or X.r)
> ans =  10.300
> ans =  6.6000
> ans =  11.500
>>> % This shows me all the values, but...
>>> foo = X(:).r
> foo =  10.300  % Assignment only gets me the first one.  I expected:
>>> foo
> foo =
> 
>    10.3000    6.6000   11.5000
> 
> Using a loop is the obvious option, but I am just wondering if there is a
> better
> way, or a better construct.
> 
> Thanks for any advice you can give me.

cellfun (@(x) x.th, <your function here>, "uni", 0)

Of course the UniformOutput value (here abbr. to "uni") depends on the
output of your function, see help cellfun.

Philip




--
Sent from: https://octave.1599824.n4.nabble.com/Octave-General-f1599825.html



reply via email to

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