Azzera filtri
Azzera filtri

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

6 visualizzazioni (ultimi 30 giorni)
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!

Risposta accettata

Matt Fig
Matt Fig il 26 Set 2012
Modificato: Matt Fig il 26 Set 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

Più risposte (1)

Thomas
Thomas il 26 Set 2012
Modificato: Thomas il 26 Set 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 Commento
Nathan
Nathan il 26 Set 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 ~ :)

Accedi per commentare.

Categorie

Scopri di più su Structures in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by