How can I Create a vector from a structure composed by tables?
Show older comments
Hi guys,
i'm new to matlab, ad i'd like to create a column vector from a structure. Every structure component is a 22x9 Table, and the vector i want to build should take a specific cell from every Tables. For exaple, i want row 22, column "RicoveratiConSintomi" of every tables.
x=struct("apr1",apr1,"apr2",apr2,"apr3",apr3,"apr4",apr4,"apr5",apr5,"apr6",apr6,"apr7",apr7);
x.apr1(22,"RicoveratiConSintomi");
n=length(fieldnames(x));
vecname=NaN(n,1);
and from here i don't really know how to write the For cycle to get what i want.
Thanks to everyone will try to help me!
Answers (1)
darova
on 12 Apr 2020
Maybe something like this
x=struct("apr1",apr1,"apr2",apr2,"apr3",apr3,"apr4",apr4,"apr5",apr5,"apr6",apr6,"apr7",apr7);
% x.apr1(22,"RicoveratiConSintomi");
n=length(fieldnames(x));
s = fieldnames(x);
vecname=NaN(n,1);
for i = 1:n
str = getfield(x,s(i));
vecname(i) = str(22,"RicoveratiConSintomi");
end
Or better be to create an array structure
x=struct("apr",{apr1 apr2 apr3 apr4 apr5 apr6 apr7});
x.apr1(22,"RicoveratiConSintomi");
n = length(x);
vecname=NaN(n,1);
for i = 1:n
vecname(i) = x(i).apr(22,"RicoveratiConSintomi");
end
1 Comment
Stephen23
on 12 Apr 2020
Good advice to use a non-scalar structure:
Categories
Find more on Structures in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!