how to extract some data from this particular structure?

조회 수: 4 (최근 30일)
Robert Jones
Robert Jones 2022년 8월 9일
편집: Stephen23 2022년 8월 9일
Hello,
I have this structure, I am trying to extract some data (preferably avoiding loops):
sasa =
1×4 cell array
{1×1 struct} {1×1 struct} {1×1 struct} {1×1 struct}
K>> sasa{1}
ans =
struct with fields:
fmt: '%05.2f'
name: 'MOVE_ALONG_AXIS'
flag: 1
prjv: 1
vals: [-20 -19 -18 -17 -16 -15 -14 -13 -12 -11 -10 -9 -8 -7 -6 -5 -4 -3 -2 -1 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20]
type: 'real'
unit: 'mm'
ref: 1
X0: [2×1 double]
LB: -20
UB: 20
magn: 0
K>> sasa{1}
sasa{2}
ans =
struct with fields:
fmt: '%05.2f'
name: 'wn'
flag: 1
prjv: 1
vals: [1×181 double]
type: 'real'
unit: 'mm'
ref: 2
X0: [2×1 double]
LB: 0.2000
UB: 2
magn: 0.5000
sasa{3}
ans =
struct with fields:
fmt: '%05.2f'
name: 'hn'
flag: 1
prjv: 1
vals: [1×991 double]
type: 'real'
unit: 'mm'
ref: 3
X0: [2×1 double]
LB: 0.1000
UB: 10
magn: 0.5000
sasa{4}
ans =
struct with fields:
fmt: '%05.2f'
name: 'pn'
flag: 1
prjv: 1
vals: [1×131 double]
type: 'real'
unit: 'mm'
ref: 4
X0: [2×1 double]
LB: 2
UB: 15
magn: 10
I would like to extarct just the names (preferably without loops), i.e. get a vector like this
M=['MOVE_ALONG_AXIS' 'wn' 'hn' 'pn']
tried a few things such as
sasa{:}.name
sasa{1:4}.name
it didn't work
Any ideas?
Thank you
  댓글 수: 1
Stephen23
Stephen23 2022년 8월 9일
Note that in MATLAB the square brackets are a concatenation operator (not a "list" operator, which MATLAB does not have), so your desired output
M=['MOVE_ALONG_AXIS' 'wn' 'hn' 'pn']
is exactly equivalent to this
M = 'MOVE_ALONG_AXISwnhnpn'

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

채택된 답변

Stephen23
Stephen23 2022년 8월 9일
편집: Stephen23 2022년 8월 9일
"Any ideas?"
Two comma-separated lists will do it. For example, where C is your cell array:
S = [C{:}]; % create non-scalar structure
N = [S.name] % then your task is this easy
Note that rather than storing lots of scalar structures in a cell array, your task would be easier if you used one non-scalar structure array right from the start. Better data deisgn makes your code simpler and more efficient.

추가 답변 (0개)

카테고리

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

제품


릴리스

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by