How to access multiple subfields of an object array?

9 visualizzazioni (ultimi 30 giorni)
Brendan
Brendan il 13 Dic 2012
Risposto: Samuel Harry il 10 Nov 2022
I have an array of objects ("g") each with a field "location". Location is a class with fields "x", "y", "z", etc. Is it possible to retrieve an array of the "x", "y" or "z" fields directly from the array "g"?
What I've done so far:
I first tried (see comment by per isakson)
g.location.x
which gives a "Dot name reference on non-scalar structure" error, even though g.location is perfectly valid for a non-scalar g.
Then I tried the following code which returns an array of location classes:
[g.location]
However, I cannot get the array of x fields as such:
[[g.location].x]
(due to an unexpected operator error at the ".x" part) without first defining
l = [g.location]
then evaluating
[l.x]
I have more subfields in x, y, and z to access and this method would be tedious to code out for all subfields.
So is there any syntax that will allow me to evaluate the array [g.location] then evaluate the array [[g.location].x] and so on for all subfields in a single line?

Risposte (2)

per isakson
per isakson il 13 Dic 2012
Modificato: per isakson il 14 Dic 2012
Try
z = g.location.x;
and if g is scalar
z = [ g.location.x ];
as you would do with structures
.
Added: 2012-12-14
I fail to find a concise description on how Matlab is supposed to behave in these cases (R2012a). However, I found some "tips" and "examples". I need something better.
The statement
z = [ g(:).location(:).x ];
or short
z = [ g.location.x ];
consists of two operations: list expansion and concatenation. I don't find the documentation on "list expansion". However, I'm positive that "list expansion" only works with the rightmost index in a train-wreck like g.location.x
Here is an experiment with list expansion on objects. I think this illustrates pretty much what is possible to achieve.
>> g34 = ArrayClass( 3, 4 );
>> g14 = ArrayClass( 1, 4 );
>> g41 = ArrayClass( 4, 1 );
where
classdef ArrayClass < handle
properties
location
end
methods
function this = ArrayClass( n, m )
if nargin == 0, return, end
this( 1, n ) = ArrayClass();
for ii = 1 : n
this( ii ).location = XYZ( m );
end
end
end
end
and where
classdef XYZ < handle
properties
x
y
z
end
methods
function this = XYZ( m )
if nargin == 0, return, end
this( 1, m ) = XYZ();
for ii = 1 : m
this( ii ).x = 10 + rand;
this( ii ).y = 20 + rand;
this( ii ).z = 30 + rand;
end
end
end
end
and some calls
>> [ g14.location.x ]
ans =
10.5211 10.6241 10.3674 10.8852
>> [ g34(3).location.x ]
ans =
10.0855 10.0292 10.4886 10.4588
>> [ g41.location.x ]
Dot name reference on non-scalar structure.
>> [ g41(2).location.x ]
ans =
10.6797
.
"[ g41.location.x ]" breaks the the rightmost index rule.
  2 Commenti
Brendan
Brendan il 13 Dic 2012
Forgot to mention in the question I had tried these already. They both give the error:
Dot name reference on non-scalar structure.

Accedi per commentare.


Samuel  Harry
Samuel Harry il 10 Nov 2022
Sound is a sophisticated skill that requires practice to develop. Sound writing is not about being able to spell every word in the dictionary or about being able to say https://www.soundsongwriting.com/ in another language. It's about delivering your thoughts in a way that your reader can connect with.

Categorie

Scopri di più su Data Type Identification in Help Center e File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by