how to extract some data from this particular structure?
3 次查看(过去 30 天)
显示 更早的评论
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
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
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 个评论
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Structures 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!