accessing multiple individual struct field values

조회 수: 2 (최근 30일)
Darren Reed
Darren Reed 2021년 5월 20일
편집: per isakson 2021년 5월 21일
I have made a structure like so;
xfr = struct('name',[],'sn',zeros(tmp,1),'xs',zeros(tmp,1),'f',[],'amp',[],'unphas',[],'lincoh',[]);
and filled the fields amp,unphas, and lincoh with 4 double values. xfr is 1x15. I need to make a scatter plot of one field vs the first value of unphas [unphas(1)]
I can easily extract all of the xs values: xfr.xs
How can I extract the first (or second, third, or fourth) value of unphas for all xfr ?
I have tried many different ways without success
a = [xfr(1:15).unphas(4)]
Expected one output from a curly brace or dot indexing
expression, but there were 15 results.

답변 (1개)

Stephen23
Stephen23 2021년 5월 20일
편집: Stephen23 2021년 5월 20일
S(1).data = [1;2;3];
S(2).data = [4;5;6];
S(3).data = [7;8;9];
n = 2;
V = arrayfun(@(s)s.data(n),S)
V = 1×3
2 5 8
or (assuming that each field contains a column vector):
M = [S.data];
V = M(2,:)
V = 1×3
2 5 8
  댓글 수: 2
Darren Reed
Darren Reed 2021년 5월 20일
I don't think I have column vectors in the structure xfr.unphas
The data is put into the structure within a double loop as this:
xfr(i).unphas(j) = unphas(mxi+ids(j,1));
Is there any other way to put the data into the xfr.unphas field to make it a column vector?
Thanks
Stephen23
Stephen23 2021년 5월 21일
The arrayfun approach does not assume anything about the orientation or size of the array. Try that first.

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

카테고리

Help CenterFile Exchange에서 Structures에 대해 자세히 알아보기

제품


릴리스

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by