How to access the subfield of the variable(field type) stored in the array

6 次查看(过去 30 天)
Hi guys, I have a little tricky prob :
I have several field type variable in the workspace and I stored them in an array using the code as :
A = whos ('*')
A(1)
ans =
name: 'x_bacyen'
size: [1 1]
bytes: 308814
class: 'struct'
global: 0
sparse: 0
complex: 0
nesting: [1x1 struct]
persistent: 0
However, since each element in the array is in fact a field variable, how can I access the subfield of each element? I tried something like this :
A(1).name.signal.value % I want to make it work as call variable x_bacyen.signal.value
but it doesn't work.. anyway to realize it ?
I will really appreciate it if anyone can give me a hint. thx!

采纳的回答

Matt Fig
Matt Fig 2012-9-26
编辑:Matt Fig 2012-9-26
This is not the way to store them in a structure. Instead you could do this:
save('myvars')
clear
A = load('myvars')
Now if you want to get the fieldnames so you can index into the variables, just do:
F = fieldnames(A);
A.(F{1}).signal.value
.
.
.
For a complete example, this works:
clear all,clc
S.r = 5;
S.malk = magic(3);
B.reff = ones(1,3);
B.help.file = 'myfile';
S.next.step = 8;
save('myvars')
clear all
A = load('myvars')
A.(F{2}).help.file % B.help.file
A.(F{1}).malk % S.malk

更多回答(1 个)

Thomas
Thomas 2012-9-26
编辑:Thomas 2012-9-26
You could try a new variable as newstring.. I'm still not clear as to why you would want to create a new variable from the value of output of a structure..
newstring=strcat(A(1).name,'.signal.value')
  1 个评论
Nathan
Nathan 2012-9-26
becoz i want to realize a loop by calling something like 'A(i).name.signal.value ' in order access the value of each field variable repeatedly .. thx anyway ~ :)

请先登录,再进行评论。

类别

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