Effacer les filtres
Effacer les filtres

access data in structure?

1 vue (au cours des 30 derniers jours)
Najmeh Eskandari
Najmeh Eskandari le 7 Fév 2019
Modifié(e) : Luna le 7 Fév 2019
Hi.
I store some data in structure :
H0=struct('h',[0,0],'Q_centroid',[0,0],'Q_action',[.5,.51,.5,.51],'Q_table',[0]);
H1=struct('h',[0,2],'Q_centroid',[5,0],'Q_action',[.5,1,.5,1],'Q_table',[1;2]);
L0=struct('H0',H0,'H1',H1);
L0Names = fieldnames(L0);
n=numel(L0Names);
i=1;
for loopIndex = 1:numel(L0Names)
stuff = L0.(L0Names{loopIndex});
w{i}=stuff;
e(i,:)=w.h;
i=i+1;
end
i want to access h in each H0 and H1 in loop and store them in a matrix 2 by 2 (e=[0 0;0 2]). how can i do this?
i use e(i,:)=w.h but there is an error.
Thanks

Réponse acceptée

Luna
Luna le 7 Fév 2019
Modifié(e) : Luna le 7 Fév 2019
You are almost there.
Try this:
H0=struct('h',[0,0],'Q_centroid',[0,0],'Q_action',[.5,.51,.5,.51],'Q_table',[0]);
H1=struct('h',[0,2],'Q_centroid',[5,0],'Q_action',[.5,1,.5,1],'Q_table',[1;2]);
L0=struct('H0',H0,'H1',H1);
L0Names = fieldnames(L0);
n=numel(L0Names);
stuff = nan(numel(L0Names),2); % preallocation
for i = 1:numel(L0Names)
stuff(i,:) = L0.(L0Names{i}).h; % goes like this: L0.H0.h, L0.H1.h,.... etc
end

Plus de réponses (0)

Catégories

En savoir plus sur Structures dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by