Arrays of structures

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Arrays of structures

Windhorn, Allen E [ACIM/LSA/MKT]
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.

Regards,
Allen
--
Allen Windhorn P.E. (Mn), CEng| Senior Principal Engineer
Leroy-Somer Americas | Kato Engineering, Inc.
2075 Howard Dr. West | North Mankato, MN 56003 | USA
T +1 507-345-2782 | F +1 507-345-2798
[hidden email] | [hidden email]



Reply | Threaded
Open this post in threaded view
|

Re: Arrays of structures

apjanke-floss


On 12/31/19 10:59 AM, 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.
>
> Regards,
> Allen
Hi Allen,

Dot-referencing into a nonscalar struct array gives you a
"comma-separated list" as a result. This is like a function with
multiple outputs. Which is why you see multiple "ans = " labels in the
output. So you need to either capture them to multiple variables, or
grab them all using concatenation with [...] or cellification with {...}.

>> foo = [X.r]
foo =
   1.030000000000000e+01   6.600000000000000e+00   1.150000000000000e+01

>>

Cheers,
Andrew


Reply | Threaded
Open this post in threaded view
|

Re: Arrays of structures

PhilipNienhuis
In reply to this post by Windhorn, Allen E [ACIM/LSA/MKT]
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