How to access multiple subfields of an object array?

조회 수: 9 (최근 30일)
Brendan
Brendan 2012년 12월 13일
답변: Samuel Harry 2022년 11월 10일
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?

답변 (2개)

per isakson
per isakson 2012년 12월 13일
편집: per isakson 2012년 12월 14일
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
Brendan
Brendan 2012년 12월 13일
Forgot to mention in the question I had tried these already. They both give the error:
Dot name reference on non-scalar structure.
per isakson
per isakson 2012년 12월 14일
See above

댓글을 달려면 로그인하십시오.


Samuel  Harry
Samuel Harry 2022년 11월 10일
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.

카테고리

Help CenterFile Exchange에서 Data Type Identification에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by