Effacer les filtres
Effacer les filtres

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

11 vues (au cours des 30 derniers jours)
Nathan
Nathan le 26 Sep 2012
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!

Réponse acceptée

Matt Fig
Matt Fig le 26 Sep 2012
Modifié(e) : Matt Fig le 26 Sep 2012
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 commentaire
Nathan
Nathan le 26 Sep 2012
Great, it works exactly as I want it to....thx very much !!

Connectez-vous pour commenter.

Plus de réponses (1)

Thomas
Thomas le 26 Sep 2012
Modifié(e) : Thomas le 26 Sep 2012
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 commentaire
Nathan
Nathan le 26 Sep 2012
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 ~ :)

Connectez-vous pour commenter.

Catégories

En savoir plus sur Structures dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by