필터 지우기
필터 지우기

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