フィルターのクリア

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

6 ビュー (過去 30 日間)
Nathan
Nathan 2012 年 9 月 26 日
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 件のコメント
Nathan
Nathan 2012 年 9 月 26 日
Great, it works exactly as I want it to....thx very much !!

サインインしてコメントする。

その他の回答 (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 Center および File ExchangeStructures についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by