[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Arrays of structures
From: |
Andrew Janke |
Subject: |
Re: Arrays of structures |
Date: |
Tue, 31 Dec 2019 11:33:55 -0500 |
User-agent: |
Mozilla/5.0 (Macintosh; Intel Mac OS X 10.14; rv:60.0) Gecko/20100101 Thunderbird/60.9.1 |
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