Implicit indexing with structures - A question

2 次查看(过去 30 天)
It is by NO WAY clear to me why this code makes sense
clear all ;
a.x = 1
b.x = 2
s = [a,b]
ANS = [s(:).x]
and why this one does not
clear all ;
a.p.x = 1
b.p.x = 11
s = [a,b]
ANS = [s(:).p.x]
The question rises from the fact that I have data stored in the second format. I can succesfully access to
s(15).p.x % x is a scalar
s(15).q.x % assuming there is also a q field
But I can't do
s(:).p.x
What is the fastest way of overcoming that?
tnx!
Mike

回答(2 个)

John D'Errico
John D'Errico 2017-8-22
编辑:John D'Errico 2017-8-22
It is not at all clear what does not make sense.
clear all ;
a.x = 1;
b.x = 2;
So, a and b are structs, each with field x.
s = [a,b];
s is a 1x2 struct. Each element of s is a struct.
ANS = [s(:).x]
ANS =
1 2
Extract the field x from each of the elements of s. The result will be a comma separated list. Combine them into one array, using [], thus horzcat.
In the second case, you have done something different.
a.p.x = 1;
b.p.x = 11;
s = [a,b];
ANS = [s(:).p.x]
Expected one output from a curly brace or dot indexing expression, but there were 2 results.
First of all, you put 11 into the second spot. ;-) Ok, that is not significant.
s s = 1×2 struct array with fields: p
So s is now a struct array, with field p.
ANS = [s(:).p]
ANS =
1×2 struct array with fields:
x
So I can extract the p field of those struct elements. Again, it is a comma separated list.
s(:).p
ans =
struct with fields:
x: 1
ans =
struct with fields:
x: 11
You cannot index a comma separated list. Remember that MATLAB works from left to right.
s(:)
ans =
2×1 struct array with fields:
p
s(:) is a struct array. I can extract the p field of that. But since the result is a comma separated list, not another struct, I cannot form s(:).p.x as you wish.
Nor can it be done as:
[s(:).p].x
[s(:).p].x
Error: Unexpected MATLAB operator.
The solution is simple enough. Break it into two consecutive operations.
temp = [s(:).p];
[temp.x]
ans =
1 11
  5 个评论
Jan
Jan 2019-6-21
编辑:Jan 2019-6-21
@Adam: In your case you distribute the 50 random elements to 50 scalar fields of the struct array. Vijay asked to setting the field Position of all 50 elements of the struct array to the same [1x2] vector. This is a difference.
@Vijay Venkatasubramanian:
[S(1:50).Position] = deal(rand(1,2))
To distribute different values to the fields:
c = num2cell(rand(1, 50));
[s(1:50).Position] = c{:};
Adam
Adam 2019-6-21
I assumed 'a random value to each of them' meant a different one to each, but certainly it is neater to use your 2nd approach, which is the one I couldn't remember, than relying on 3rd party functions :)

请先登录,再进行评论。


Adam
Adam 2017-8-22
编辑:Adam 2017-8-22
You can just do it in two lines:
stuff = [s.p];
result = [ stuff.x ];
(yes, I always have trouble naming random intermediate results that I don't care about!)
Incidentally, the (:) is un-necessary.

类别

Help CenterFile Exchange 中查找有关 Structures 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by